package com.eurosms.speedtest44;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;
import fr.bmartel.speedtest.SpeedTestReport;
import fr.bmartel.speedtest.SpeedTestSocket;


public class FTPSpeedTest extends AsyncTask<Void, Void, String> {

    
    private String host="speedtest.tele2.net";
    private String file="/550MB.zip";
    private int port=21;
    private int reportInterval=1000;
    private int fixedTime = 10000;
    private String user = "anonymous";
    private String password = "";
    private MainActivity activity;
    private boolean auth = false;

    private SpeedTestSocket speedTestSocket = new SpeedTestSocket();

   
    public FTPSpeedTest(MainActivity view) {
        activity = view;
        speedTestSocket.addSpeedTestListener(new FTPSpeedTestListener(activity));
        host=((EditText) view.findViewById(R.id.ehost)).getText().toString();
        port = Integer.parseInt(((EditText) view.findViewById(R.id.eport)).getText().toString());
        file=((EditText) view.findViewById(R.id.efile)).getText().toString();
  

        String u =((EditText) activity.findViewById(R.id.euser)).getText().toString();
        if (u != null && !u.isEmpty()){
            user = u;
            password = ((EditText) activity.findViewById(R.id.epass)).getText().toString();
            auth = true;
        }

    }

    
    public void reset(){
        speedTestSocket.shutdownAndWait();
        speedTestSocket.clearListeners();
        speedTestSocket.addSpeedTestListener(new FTPSpeedTestListener(activity));
        host=((EditText) activity.findViewById(R.id.ehost)).getText().toString();
        port = Integer.parseInt(((EditText) activity.findViewById(R.id.eport)).getText().toString());
        file=((EditText) activity.findViewById(R.id.efile)).getText().toString();

        String u =((EditText) activity.findViewById(R.id.euser)).getText().toString();
        if (u != null && !u.isEmpty()){
            user = u;
            password = ((EditText) activity.findViewById(R.id.epass)).getText().toString();
        }


    }

    public SpeedTestReport getReport(){
        return speedTestSocket.getLiveDownloadReport();
    }

    
    @Override
    protected String doInBackground(Void... voids) {

        if (auth) {
            speedTestSocket.startFtpDownload(host, port, file, user, password);
        } else {
            speedTestSocket.startFtpDownload(host, file);
        }

        return "OK";
    }

    @Override
    public String toString() {
        return "FTPSpeedTest{" +
                "host='" + host + '\'' +
                ", file='" + file + '\'' +
                ", port=" + port +
                ", reportInterval=" + reportInterval +
                ", fixedTime=" + fixedTime +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                ", activity=" + activity +
                ", speedTestSocket=" + speedTestSocket +
                '}';
    }
}
