package com.eurosms.speedtest44;

import android.util.Log;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import fr.bmartel.speedtest.SpeedTestReport;
import fr.bmartel.speedtest.inter.IRepeatListener;

/**
 * This app does work yes
 * not used but probably  later
 */
public class FTPRepeatListener implements IRepeatListener {
    private MainActivity activity;
    private TextView downSpeed;

    private Set<BigDecimal> speeds = new HashSet<>();

    public FTPRepeatListener(MainActivity activity) {
  
        this.activity = activity;
        this.downSpeed = (TextView) activity.findViewById(R.id.ts1mb);


       
    }


    @Override
    public void onFinish(SpeedTestReport report) {

        int size = speeds.size();
        BigDecimal sum = new BigDecimal(0);
        for (BigDecimal d : speeds){
            sum = sum.add(d);
        }

        sum = sum.divide(new BigDecimal(speeds.size()), BigDecimal.ROUND_FLOOR);



        for (int i = 0; i < size/3; i++){
            BigDecimal min = new BigDecimal(680826799);
            for (BigDecimal d: speeds){
                if (d.compareTo(min) == -1){
                    min = d;
                }
            }
            speeds.remove(min);
        }
        sum = new BigDecimal(0);
        for (BigDecimal d : speeds){
            sum = sum.add(d);
        }

        sum = sum.divide(new BigDecimal(speeds.size()), BigDecimal.ROUND_FLOOR);
        
    size = speeds.size();
        for (int i = 0; i < size/10; i++){
            BigDecimal max = new BigDecimal(-1);
            for (BigDecimal d: speeds){
                if (d.compareTo(max) == 1){
                    max = d;
                }
            }
            speeds.remove(max);
        }
        sum = new BigDecimal(0);
        for (BigDecimal d : speeds){
            sum = sum.add(d);
        }

        sum = sum.divide(new BigDecimal(speeds.size()), BigDecimal.ROUND_FLOOR);
   

        activity.setText(downSpeed, "FINISHED: " + (report.getTransferRateBit().multiply(new BigDecimal(1e-6))).setScale(4, BigDecimal.ROUND_FLOOR));

    }

    @Override
    public void onReport(SpeedTestReport report) {

        speeds.add(report.getTransferRateBit());
        activity.setText(downSpeed, "REPORT: " + (report.getTransferRateBit().multiply(new BigDecimal(1e-6))).setScale(4, BigDecimal.ROUND_FLOOR));
    }
}
