package it.unimi.di.laren.smartwash.Controller;

import it.unimi.di.laren.smartwash.R;
import it.unimi.di.laren.smartwash.View.ElaborationActivity;
import it.unimi.di.laren.smartwash.View.SubmitActivity;
import android.content.Intent;
import android.graphics.Color;
import android.opengl.Visibility;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class Elaboration_Controller implements OnClickListener{
	private ElaborationActivity elabActivity = null;

	public Elaboration_Controller(ElaborationActivity activity) {
		// TODO Auto-generated constructor stub
		elabActivity = activity;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.arrowLeft:
			if (elabActivity.fase==1) {
				elabActivity.fase=1;
			}
			else if(elabActivity.fase==2){
				elabActivity.fasi1.setVisibility(View.VISIBLE);
				elabActivity.fasi2.setVisibility(View.GONE);
				elabActivity.fasi3.setVisibility(View.GONE);
				elabActivity.fasi4.setVisibility(View.GONE);
				elabActivity.fasi5.setVisibility(View.GONE);
				elabActivity.one.setTextColor(Color.MAGENTA);
				elabActivity.two.setTextColor(Color.BLACK);
				elabActivity.three.setTextColor(Color.BLACK);
				elabActivity.four.setTextColor(Color.BLACK);
				elabActivity.five.setTextColor(Color.BLACK);
				elabActivity.fase=1;
			}
			else if(elabActivity.fase==3){
				elabActivity.fasi1.setVisibility(View.GONE);
				elabActivity.fasi2.setVisibility(View.VISIBLE);
				elabActivity.fasi3.setVisibility(View.GONE);
				elabActivity.fasi4.setVisibility(View.GONE);
				elabActivity.fasi5.setVisibility(View.GONE);
				elabActivity.one.setTextColor(Color.BLACK);
				elabActivity.two.setTextColor(Color.MAGENTA);
				elabActivity.three.setTextColor(Color.BLACK);
				elabActivity.four.setTextColor(Color.BLACK);
				elabActivity.five.setTextColor(Color.BLACK);
				elabActivity.fase=2;
			}
			else if(elabActivity.fase==4){
				elabActivity.fasi1.setVisibility(View.GONE);
				elabActivity.fasi2.setVisibility(View.GONE);
				elabActivity.fasi3.setVisibility(View.VISIBLE);
				elabActivity.fasi4.setVisibility(View.GONE);
				elabActivity.fasi5.setVisibility(View.GONE);
				elabActivity.one.setTextColor(Color.BLACK);
				elabActivity.two.setTextColor(Color.BLACK);
				elabActivity.three.setTextColor(Color.MAGENTA);
				elabActivity.four.setTextColor(Color.BLACK);
				elabActivity.five.setTextColor(Color.BLACK);
				elabActivity.fase=3;
			}
			else if(elabActivity.fase==5){
				elabActivity.fasi1.setVisibility(View.GONE);
				elabActivity.fasi2.setVisibility(View.GONE);
				elabActivity.fasi3.setVisibility(View.GONE);
				elabActivity.fasi4.setVisibility(View.VISIBLE);
				elabActivity.fasi5.setVisibility(View.GONE);
				elabActivity.one.setTextColor(Color.BLACK);
				elabActivity.two.setTextColor(Color.BLACK);
				elabActivity.three.setTextColor(Color.BLACK);
				elabActivity.four.setTextColor(Color.MAGENTA);
				elabActivity.five.setTextColor(Color.BLACK);
				elabActivity.fase=4;
			}
			break;
		case R.id.arrowRight:
			if (elabActivity.fase==1) {
				elabActivity.fasi1.setVisibility(View.GONE);
				elabActivity.fasi2.setVisibility(View.VISIBLE);
				elabActivity.fasi3.setVisibility(View.GONE);
				elabActivity.fasi4.setVisibility(View.GONE);
				elabActivity.fasi5.setVisibility(View.GONE);
				elabActivity.one.setTextColor(Color.BLACK);
				elabActivity.two.setTextColor(Color.MAGENTA);
				elabActivity.three.setTextColor(Color.BLACK);
				elabActivity.four.setTextColor(Color.BLACK);
				elabActivity.five.setTextColor(Color.BLACK);
				elabActivity.fase=2;;
			}
			else if(elabActivity.fase==2){
				elabActivity.fasi1.setVisibility(View.GONE);
				elabActivity.fasi2.setVisibility(View.GONE);
				elabActivity.fasi3.setVisibility(View.VISIBLE);
				elabActivity.fasi4.setVisibility(View.GONE);
				elabActivity.fasi5.setVisibility(View.GONE);
				elabActivity.one.setTextColor(Color.BLACK);
				elabActivity.two.setTextColor(Color.BLACK);
				elabActivity.three.setTextColor(Color.MAGENTA);
				elabActivity.four.setTextColor(Color.BLACK);
				elabActivity.five.setTextColor(Color.BLACK);
				elabActivity.fase=3;
			}
			else if(elabActivity.fase==3){
				elabActivity.fasi1.setVisibility(View.GONE);
				elabActivity.fasi2.setVisibility(View.GONE);
				elabActivity.fasi3.setVisibility(View.GONE);
				elabActivity.fasi4.setVisibility(View.VISIBLE);
				elabActivity.fasi5.setVisibility(View.GONE);
				elabActivity.one.setTextColor(Color.BLACK);
				elabActivity.two.setTextColor(Color.BLACK);
				elabActivity.three.setTextColor(Color.BLACK);
				elabActivity.four.setTextColor(Color.MAGENTA);
				elabActivity.five.setTextColor(Color.BLACK);
				elabActivity.fase=4;
			}
			else if(elabActivity.fase==4){
				elabActivity.fasi1.setVisibility(View.GONE);
				elabActivity.fasi2.setVisibility(View.GONE);
				elabActivity.fasi3.setVisibility(View.GONE);
				elabActivity.fasi4.setVisibility(View.GONE);
				elabActivity.fasi5.setVisibility(View.VISIBLE);
				elabActivity.one.setTextColor(Color.BLACK);
				elabActivity.two.setTextColor(Color.BLACK);
				elabActivity.three.setTextColor(Color.BLACK);
				elabActivity.four.setTextColor(Color.BLACK);
				elabActivity.five.setTextColor(Color.MAGENTA);
				elabActivity.fase=5;
			}
			else if(elabActivity.fase==5){
				elabActivity.fase=5;
			}
			break;
		case R.id.SubmitBtn:
			Log.i("p1", ":" + elabActivity.progressChanged1);
			Log.i("p2", ":" + elabActivity.progressChanged2);
			Log.i("p3", ":" + elabActivity.progressChanged3);
			Log.i("p4", ":" + elabActivity.progressChanged4);
			Log.i("p5", ":" + elabActivity.progressChanged5);
			Log.i("p6", ":" + elabActivity.progressChanged6);
			Log.i("p7", ":" + elabActivity.progressChanged7);
			Log.i("p8", ":" + elabActivity.progressChanged8);
			Log.i("p9", ":" + elabActivity.progressChanged9);
			Log.i("p10", ":" + elabActivity.progressChanged10);
			
			
			float somma =(float)((float) (Math.pow((elabActivity.progressChanged3)/1000, 2)*elabActivity.progressChanged5) + (Math.pow((elabActivity.progressChanged7)/1000, 2)*elabActivity.progressChanged8) + (Math.pow((elabActivity.progressChanged9)/1000, 2)*elabActivity.progressChanged10) + (10*Math.pow((elabActivity.progressChanged2)*0.01,2))*elabActivity.progressChanged1);
			float k = (float)1/(elabActivity.valkWh(elabActivity.progressChanged3, elabActivity.progressChanged7, elabActivity.progressChanged9, elabActivity.progressChanged5, elabActivity.progressChanged8, elabActivity.progressChanged10, elabActivity.progressChanged2, elabActivity.progressChanged1));
			float h = elabActivity.totH2O(elabActivity.progressChanged1, elabActivity.progressChanged6);
			float c = elabActivity.totCO2(elabActivity.cod);
			Log.i("totH", ":" + h);
			Log.i("totC", ":" + c);
			Log.i("kwh", ":" + somma);
			Log.i("totK", ":" + k);
			float eff1 = elabActivity.calcEff_i(elabActivity.database.water1, elabActivity.progressChanged1, elabActivity.var_water);
			float eff2 = elabActivity.calcEff_i(elabActivity.database.temp, elabActivity.progressChanged1, elabActivity.var_temp);
			float eff3 = elabActivity.calcEff_i(elabActivity.database.spin1, elabActivity.progressChanged1, elabActivity.var_spin);
			float eff4 = elabActivity.calcEff_i(elabActivity.database.soap, elabActivity.progressChanged1, elabActivity.var_soap);
			float eff5 = elabActivity.calcEff_i(elabActivity.database.time1, elabActivity.progressChanged1, elabActivity.var_time);
			float eff6 = elabActivity.calcEff_i(elabActivity.database.water2, elabActivity.progressChanged1, elabActivity.var_water);
			float eff7 = elabActivity.calcEff_i(elabActivity.database.spin2, elabActivity.progressChanged1, elabActivity.var_spin);
			float eff8 = elabActivity.calcEff_i(elabActivity.database.time2, elabActivity.progressChanged1, elabActivity.var_time);
			float eff9 = elabActivity.calcEff_i(elabActivity.database.spin3, elabActivity.progressChanged1, elabActivity.var_spin);
			float eff10 = elabActivity.calcEff_i(elabActivity.database.time3, elabActivity.progressChanged1, elabActivity.var_time);
			float tE = elabActivity.sommaEff(eff1, eff2, eff3, eff4, eff5, eff6, eff7, eff8, eff9, eff10);
			float ttt = (float) Math.exp(-((float)tE/50000));
			float e = elabActivity.totEff(ttt);
			Log.i("somm", ":" + tE);
			Log.i("sommlog", ":" + ttt);
			Log.i("Eff", ":" + e);
			Log.i("imei", ":" + elabActivity.imei);
			
			
			float tot = k+h+c+e;
			String result = "null";
			result = elabActivity.sendHttpScore(tot, elabActivity.sce);
			Log.i("result", ":" + result);
			if(result != "null"){
			String ranking = "null";
			String position = "null";
			int p = result.indexOf(",");
			int s = result.indexOf(".");
			Log.i("p", ":" + p);
			ranking = result.substring(0, p);
			position = result.substring(p+1);
			Log.i("ranksplit", ":" + ranking);
			Log.i("possplit", ":" + position);
			Intent showSubmit = new Intent(elabActivity.getApplicationContext(),
					SubmitActivity.class);
			showSubmit.putExtra("KWH", k);
			showSubmit.putExtra("H2O", h);
			showSubmit.putExtra("CO2", c);
			showSubmit.putExtra("EFF", e);
			showSubmit.putExtra("TOT", tot);
			showSubmit.putExtra("RANKING", ranking);
			showSubmit.putExtra("IMEI", elabActivity.imei);
			elabActivity.startActivity(showSubmit);
			}
			else{
				Toast.makeText(elabActivity, "Select all the values or no internet connection", Toast.LENGTH_SHORT).show();
			}
			
			break;
		default:
			break;
		}
		
	}

}
