package com.example.karanchitnis.solaryou;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.ViewGroup.LayoutParams;

public class Conversion extends Activity {
    private String inputAgeWeight = "Age";
    private double inputVal = -1;
    private String inputUnit = "Years";
    private double inputDayConversion = 0;
    private double inputYearConversion = 0;
    private double inputLbConversion = 0;
    private String outputUnit = "Years";
    private double outputDayConversion = 0;
    private double outputYearConversion = 0;
    private double outputLbConversion = 0;

    public static final double EARTH_YEAR_TO_MERCURY_DAYS = 87.96;
    public static final double EARTH_YEAR_TO_VENUS_DAYS = 224.68;
    public static final double EARTH_YEAR_TO_EARTH_DAYS = 365.26;
    public static final double EARTH_YEAR_TO_MARS_DAYS = 686.96;
    public static final double EARTH_YEAR_TO_JUPITER_DAYS = EARTH_YEAR_TO_EARTH_DAYS * 11.862;
    public static final double EARTH_YEAR_TO_SATURN_DAYS = EARTH_YEAR_TO_EARTH_DAYS * 29.456;
    public static final double EARTH_YEAR_TO_URANUS_DAYS = EARTH_YEAR_TO_EARTH_DAYS * 84.07;
    public static final double EARTH_YEAR_TO_NEPTUNE_DAYS = EARTH_YEAR_TO_EARTH_DAYS * 164.81;

    public static final double EARTH_DAY_TO_MERCURY_DAYS = 58.646;
    public static final double EARTH_DAY_TO_VENUS_DAYS = 116.75;
    public static final double EARTH_DAY_TO_EARTH_DAYS = 0.997;
    public static final double EARTH_DAY_TO_MARS_DAYS = 1.028;
    public static final double EARTH_DAY_TO_JUPITER_DAYS = 0.414;
    public static final double EARTH_DAY_TO_SATURN_DAYS = 0.444;
    public static final double EARTH_DAY_TO_URANUS_DAYS = 0.718;
    public static final double EARTH_DAY_TO_NEPTUNE_DAYS = 0.671;

    public static final double EARTH_LB_TO_MERCURY_LBS = 0.38;
    public static final double EARTH_LB_TO_VENUS_LBS = 0.91;
    public static final double EARTH_LB_TO_EARTH_LBS = 1.0;
    public static final double EARTH_LB_TO_MARS_LBS = 0.38;
    public static final double EARTH_LB_TO_JUPITER_LBS = 2.36;
    public static final double EARTH_LB_TO_SATURN_LBS = 0.91;
    public static final double EARTH_LB_TO_URANUS_LBS = 0.89;
    public static final double EARTH_LB_TO_NEPTUNE_LBS = 1.12;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversion);

        addAgeWeightRadioListener();
        addInputOutputRadioListener();
        addElementsToSpinner();
        addSpinnerListener();
        addInputNumberListener();
    }

    public void addAgeWeightRadioListener() {
        RadioGroup ageWeightGroup = (RadioGroup) findViewById(R.id.ageWeightGroup);
        ageWeightGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioGroup unitsInputGroup = (RadioGroup) findViewById(R.id.unitsInputGroup);
                RadioGroup unitsOutputGroup = (RadioGroup) findViewById(R.id.unitsOutputGroup);
                switch (checkedId) {
                    case R.id.ageRadio:
                        inputAgeWeight = "Age";
                        ((RadioButton) unitsInputGroup.getChildAt(0)).setText("years");
                        ((RadioButton) unitsInputGroup.getChildAt(1)).setText("days");
                        ((RadioButton) unitsOutputGroup.getChildAt(0)).setText("years");
                        ((RadioButton) unitsOutputGroup.getChildAt(1)).setText("days");
                        break;
                    case R.id.weightRadio:
                        inputAgeWeight = "Weight";
                        ((RadioButton) unitsInputGroup.getChildAt(0)).setText("lbs");
                        ((RadioButton) unitsInputGroup.getChildAt(1)).setText("kgs");
                        ((RadioButton) unitsOutputGroup.getChildAt(0)).setText("lbs");
                        ((RadioButton) unitsOutputGroup.getChildAt(1)).setText("kgs");
                        break;
                }
                getCheckedButtons();
                setOutputNumber();
            }
        });
    }

    public void getCheckedButtons() {
        RadioButton inputYearsOrLbs = (RadioButton) findViewById(R.id.yearsRadio);
        RadioButton inputDaysOrKgs = (RadioButton) findViewById(R.id.daysRadio);
        RadioButton outputYearsOrLbs = (RadioButton) findViewById(R.id.yearsOutputRadio);
        RadioButton outputDaysOrKgs = (RadioButton) findViewById(R.id.daysOutputRadio);
        if (inputYearsOrLbs.isChecked()) {
            if (inputYearsOrLbs.getText().equals("years")) {
                inputUnit = "Years";
            }
            else {
                inputUnit = "Lbs";
            }
        }
        else if (inputDaysOrKgs.isChecked()) {
            if (inputDaysOrKgs.getText().equals("days")) {
                inputUnit = "Days";
            }
            else {
                inputUnit = "Kgs";
            }
        }

        if (outputYearsOrLbs.isChecked()) {
            if (outputYearsOrLbs.getText().equals("years")) {
                outputUnit = "Years";
            }
            else {
                outputUnit = "Lbs";
            }
        }
        else if (outputDaysOrKgs.isChecked()) {
            if (outputDaysOrKgs.getText().equals("days")) {
                outputUnit = "Days";
            }
            else {
                outputUnit = "Kgs";
            }
        }
    }

    public void addInputOutputRadioListener() {
        RadioGroup inputYearsOrDays = (RadioGroup) findViewById(R.id.unitsInputGroup);
        inputYearsOrDays.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton inputYearsOrLbs = (RadioButton) findViewById(R.id.yearsRadio);
                RadioButton inputDaysOrKgs = (RadioButton) findViewById(R.id.daysRadio);
                switch (checkedId) {
                    case R.id.yearsRadio:
                        if (inputYearsOrLbs.getText().equals("years")) {
                            inputUnit = "Years";
                        }
                        else {
                            inputUnit = "Lbs";
                        }
                        break;
                    case R.id.daysRadio:
                        if (inputDaysOrKgs.getText().equals("days")) {
                            inputUnit = "Days";
                        }
                        else {
                            inputUnit = "Kgs";
                        }
                        break;
                }
                setOutputNumber();
            }
        });
        RadioGroup outputYearsOrLbs = (RadioGroup) findViewById(R.id.unitsOutputGroup);
        outputYearsOrLbs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton outputYearsOrLbs = (RadioButton) findViewById(R.id.yearsOutputRadio);
                RadioButton outputDaysOrKgs = (RadioButton) findViewById(R.id.daysOutputRadio);
                switch (checkedId) {
                    case R.id.yearsOutputRadio:
                        if (outputYearsOrLbs.getText().equals("years")) {
                            outputUnit = "Years";
                        }
                        else {
                            outputUnit = "Lbs";
                        }
                        break;
                    case R.id.daysOutputRadio:
                        if (outputDaysOrKgs.getText().equals("days")) {
                            outputUnit = "Days";
                        }
                        else {
                            outputUnit = "Kgs";
                        }
                        break;
                }
                setOutputNumber();
            }
        });
    }

    public void addSpinnerListener() {
        Spinner inputSpinner = (Spinner) findViewById(R.id.inputPlanetSpinner);
        inputSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                ImageView imgView = (ImageView) findViewById(R.id.planetImage);
                switch ((int) id) {
                    case 0:
                        imgView.setImageResource(R.drawable.mercury);
                        inputDayConversion = EARTH_DAY_TO_MERCURY_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_MERCURY_DAYS;
                        inputLbConversion = EARTH_LB_TO_MERCURY_LBS;
                        break;
                    case 1:
                        imgView.setImageResource(R.drawable.venus);
                        inputDayConversion = EARTH_DAY_TO_VENUS_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_VENUS_DAYS;
                        inputLbConversion = EARTH_LB_TO_VENUS_LBS;
                        break;
                    case 2:
                        imgView.setImageResource(R.drawable.earth);
                        inputDayConversion = EARTH_DAY_TO_EARTH_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_EARTH_DAYS;
                        inputLbConversion = EARTH_LB_TO_EARTH_LBS;
                        break;
                    case 3:
                        imgView.setImageResource(R.drawable.mars);
                        inputDayConversion = EARTH_DAY_TO_MARS_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_MARS_DAYS;
                        inputLbConversion = EARTH_LB_TO_MARS_LBS;
                        break;
                    case 4:
                        imgView.setImageResource(R.drawable.jupiter);
                        inputDayConversion = EARTH_DAY_TO_JUPITER_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_JUPITER_DAYS;
                        inputLbConversion = EARTH_LB_TO_JUPITER_LBS;
                        break;
                    case 5:
                        imgView.setImageResource(R.drawable.saturn);
                        inputDayConversion = EARTH_DAY_TO_SATURN_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_SATURN_DAYS;
                        inputLbConversion = EARTH_LB_TO_SATURN_LBS;
                        break;
                    case 6:
                        imgView.setImageResource(R.drawable.uranus);
                        inputDayConversion = EARTH_DAY_TO_URANUS_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_URANUS_DAYS;
                        inputLbConversion = EARTH_LB_TO_URANUS_LBS;
                        break;
                    case 7:
                        imgView.setImageResource(R.drawable.neptune);
                        inputDayConversion = EARTH_DAY_TO_NEPTUNE_DAYS;
                        inputYearConversion = EARTH_YEAR_TO_NEPTUNE_DAYS;
                        inputLbConversion = EARTH_LB_TO_NEPTUNE_LBS;
                        break;
                }
                Display getOrient = getWindowManager().getDefaultDisplay();
                if(getOrient.getWidth() < getOrient.getHeight()){
                    imgView.getLayoutParams().width = 200;
                    imgView.getLayoutParams().height = 200;
                }
                else {
                    imgView.getLayoutParams().width = 75;
                    imgView.getLayoutParams().height = 75;
                    View inputNumber = (View)findViewById(R.id.inputNumber);
                    LayoutParams params = inputNumber.getLayoutParams();
                    params.width = 850;
                    inputNumber.setLayoutParams(params);
                }
                setOutputNumber();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {

            }
        });
        Spinner outputSpinner = (Spinner) findViewById(R.id.outputPlanetSpinner);
        outputSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                ImageView imgView = (ImageView) findViewById(R.id.planetOutputImage);
                switch ((int) id) {
                    case 0:
                        imgView.setImageResource(R.drawable.mercury);
                        outputDayConversion = EARTH_DAY_TO_MERCURY_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_MERCURY_DAYS;
                        outputLbConversion = EARTH_LB_TO_MERCURY_LBS;
                        break;
                    case 1:
                        imgView.setImageResource(R.drawable.venus);
                        outputDayConversion = EARTH_DAY_TO_VENUS_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_VENUS_DAYS;
                        outputLbConversion = EARTH_LB_TO_VENUS_LBS;
                        break;
                    case 2:
                        imgView.setImageResource(R.drawable.earth);
                        outputDayConversion = EARTH_DAY_TO_EARTH_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_EARTH_DAYS;
                        outputLbConversion = EARTH_LB_TO_EARTH_LBS;
                        break;
                    case 3:
                        imgView.setImageResource(R.drawable.mars);
                        outputDayConversion = EARTH_DAY_TO_MARS_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_MARS_DAYS;
                        outputLbConversion = EARTH_LB_TO_MARS_LBS;
                        break;
                    case 4:
                        imgView.setImageResource(R.drawable.jupiter);
                        outputDayConversion = EARTH_DAY_TO_JUPITER_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_JUPITER_DAYS;
                        outputLbConversion = EARTH_LB_TO_JUPITER_LBS;
                        break;
                    case 5:
                        imgView.setImageResource(R.drawable.saturn);
                        outputDayConversion = EARTH_DAY_TO_SATURN_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_SATURN_DAYS;
                        outputLbConversion = EARTH_LB_TO_SATURN_LBS;
                        break;
                    case 6:
                        imgView.setImageResource(R.drawable.uranus);
                        outputDayConversion = EARTH_DAY_TO_URANUS_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_URANUS_DAYS;
                        outputLbConversion = EARTH_LB_TO_URANUS_LBS;
                        break;
                    case 7:
                        imgView.setImageResource(R.drawable.neptune);
                        outputDayConversion = EARTH_DAY_TO_NEPTUNE_DAYS;
                        outputYearConversion = EARTH_YEAR_TO_NEPTUNE_DAYS;
                        outputLbConversion = EARTH_LB_TO_NEPTUNE_LBS;
                        break;
                }
                Display getOrient = getWindowManager().getDefaultDisplay();
                if(getOrient.getWidth() < getOrient.getHeight()){
                    imgView.getLayoutParams().width = 200;
                    imgView.getLayoutParams().height = 200;
                }
                else {
                    imgView.getLayoutParams().width = 80;
                    imgView.getLayoutParams().height = 80;
                    View outputNumber = (View)findViewById(R.id.outputNumber);
                    LayoutParams params = outputNumber.getLayoutParams();
                    params.width = 850;
                    outputNumber.setLayoutParams(params);
                }
                setOutputNumber();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {

            }
        });
    }

    public void addElementsToSpinner() {
        Spinner inputSpinner = (Spinner) findViewById(R.id.inputPlanetSpinner);
        Spinner outputSpinner = (Spinner) findViewById(R.id.outputPlanetSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        inputSpinner.setAdapter(adapter);
        outputSpinner.setAdapter(adapter);
    }

    public void addInputNumberListener() {
        EditText inputNumber = (EditText) findViewById(R.id.inputNumber);
        inputNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable mEdit) {
                String inputText = mEdit.toString();
                if (inputText.length() == 0) {
                    TextView outputText = (TextView) findViewById(R.id.outputNumber);
                    inputVal = 0;
                    outputText.setText("");
                }
                else if (inputText.substring(inputText.length() - 1) != ".") {
                    inputVal = Double.parseDouble(mEdit.toString());
                    setOutputNumber();
                }
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after){}

            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });
    }

    public void setOutputNumber() {
        EditText inputText = (EditText) findViewById(R.id.inputNumber);
        if (!inputText.getText().equals("") && inputVal >= 0) {
            TextView outputNumber = (TextView)findViewById(R.id.outputNumber);
            double outputVal = 0;
            if (inputAgeWeight.equals("Age")) {
                if ((inputUnit.equals("Years") && outputUnit.equals("Years"))) {
                    outputVal = inputVal * (inputYearConversion / outputYearConversion);
                }
                else if (inputUnit.equals("Years") && outputUnit.equals("Days")) {
                    outputVal = inputVal * (inputYearConversion / outputDayConversion);
                }
                else if (inputUnit.equals("Days") && outputUnit.equals("Years")) {
                    outputVal = inputVal * (inputDayConversion / outputYearConversion);
                }
                else if (inputUnit.equals("Days") && outputUnit.equals("Days")) {
                    outputVal = inputVal * (inputDayConversion / outputDayConversion);
                }
            }
            else {
                if ((inputUnit.equals("Lbs") && outputUnit.equals("Lbs"))) {
                    outputVal = inputVal * (outputLbConversion / inputLbConversion);
                }
                else if (inputUnit.equals("Lbs") && outputUnit.equals("Kgs")) {
                    outputVal = inputVal * (outputLbConversion / inputLbConversion) * 0.453592;
                }
                else if (inputUnit.equals("Kgs") && outputUnit.equals("Lbs")) {
                    outputVal = (inputVal * (outputLbConversion / inputLbConversion)) / 0.453592;
                }
                else if (inputUnit.equals("Kgs") && outputUnit.equals("Kgs")) {
                    outputVal = inputVal * (outputLbConversion / inputLbConversion);
                }
            }
            outputVal = (double)Math.round(outputVal * 1000) / 1000;
            outputNumber.setText(String.valueOf(outputVal));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.conversion, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
