package fr.iutfbleau.dev45.wamster.ex03temperatures;

import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.view.KeyEvent;

public class Evenement implements TextView.OnEditorActionListener {

    private final EditText degreC;
    private final EditText degreF;

    public Evenement(EditText degreC, EditText degreF){
        this.degreC = degreC;
        this.degreF = degreF;
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        Log.v("test","entrer");
        EditText source = (EditText) textView;
        String texteSource = String.valueOf(source.getText());
        if (source.getId() == this.degreC.getId()){
            degreF.setText(Evenement.convertionCToF(texteSource));
            return true;
        }
        if (source.getId() == this.degreF.getId()){
            degreC.setText(Evenement.convertionFToC(texteSource));
            return true;
        }
        return false;
    }

    private static String convertionCToF(String c){
        double cDouble = Double.parseDouble(c);
        double fDouble = cDouble*9d/5d + 32d;
        return fDouble+"";
    }

    private static String convertionFToC(String f){
        double fDouble = Double.parseDouble(f);
        double cDouble = ((fDouble-32d)*5d)/9d;
        return cDouble+"";
    }
}
