package com.eurosms.speedtest44;

import android.util.Log;
import android.widget.TextView;
import fr.bmartel.speedtest.SpeedTestReport;
import fr.bmartel.speedtest.inter.ISpeedTestListener;
import fr.bmartel.speedtest.model.SpeedTestError;

public class FTPSpeedTestListener implements ISpeedTestListener {

    private MainActivity activity;
    private TextView downSpeed;
    private TextView dwSize;
    private TextView progress;
    private int lastgc = 0;

  /
    public static String humanReadableBiteCount(long bites, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bites < unit) return bites + " b";
        int exp = (int) (Math.log(bites) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
        return String.format("%.3f %sb", bites / Math.pow(unit, exp), pre);
    }

    
    public static String humanReadableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
        return String.format("%.3f %sB", bytes / Math.pow(unit, exp) / 8, pre);
    }

   
    public FTPSpeedTestListener(MainActivity activity) {
      
        this.activity = activity;
        this.downSpeed = (TextView) activity.findViewById(R.id.ts1mb);
        this.dwSize = (TextView) activity.findViewById(R.id.ts5mb);
        this.progress = (TextView) activity.findViewById(R.id.ts10mb);

      
    }


  
    @Override
    public void onDownloadFinished(SpeedTestReport report) {
      
     
        activity.setText(downSpeed, (humanReadableByteCount(report.getTransferRateBit().longValue(),false))
        +" / " + (humanReadableBiteCount(report.getTransferRateBit().longValue(),false))
        );
        activity.setText(dwSize,""+humanReadableByteCount(report.getTotalPacketSize()*8, false));
        activity.setText(progress, "100%");
        lastgc = 0;
        System.gc();
    }

  
    @Override
    public void onDownloadError(SpeedTestError speedTestError, String errorMessage) {
        
        activity.alert("Download ERROR", errorMessage + "\n" + speedTestError.name());
    }

    @Override
    public void onUploadFinished(SpeedTestReport report) {
        
   
    }

    @Override
    public void onUploadError(SpeedTestError speedTestError, String errorMessage) {
        activity.alert("Download ERROR", errorMessage + "\n" + speedTestError.name());
    }

  rride
    public void onDownloadProgress(float percent, SpeedTestReport report) {
        

        int pc = (int) percent;

        activity.setText(downSpeed, (humanReadableByteCount(report.getTransferRateBit().longValue(),false))
                +" / " + (humanReadableBiteCount(report.getTransferRateBit().longValue(),false))
        );
        activity.setText(dwSize,""+humanReadableByteCount(report.getTemporaryPacketSize()*8, false));
        activity.setText(progress, percent + "%");
        if (pc % 5 == 0 && lastgc != pc){
//         
            lastgc = pc ;


        }

    }

    @Override
    public void onUploadProgress(float percent, SpeedTestReport report) {
        
        
    }

   
    @Override
    public void onInterruption() {
       
        activity.alert("Download ERROR", "triggered when forceStopTask is called");

    }
}
