package com.sliit.smartserviceassistant;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileOutputStream;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class PredictedMileage extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    EditText textName6;
    EditText textName7;
    EditText textName8;
    EditText textName9;
    EditText textName10;
    EditText textName11;
    EditText textName12;
    EditText textName15;

    public static int cal2;


    Switch simpleSwitch1, simpleSwitch2;
    public String text6,text7,text8,text9,text10,text11,text12;
    Spinner spinner6,spinner7,spinner8,spinner9,spinner10,spinner11,spinner12;
    int oilFilter,engine_oil,wash_PD,DA_PF,WA_B,AC_F,cool_ent,Break_Fluid,brake_Pads,F_F,SparkPlug,clut_ch,Break_And_Clutch_Oil,transmission_fluid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_predicted_mileage);
        String g;
        Button add, clear;




        textName6 = (EditText) findViewById(R.id.EditTxtVehicleType);
        textName7 = (EditText) findViewById(R.id.EditTxtVehicleBrand);
        textName8 = (EditText) findViewById(R.id.EditTxtVehicleModel);
        textName9 = (EditText) findViewById(R.id.EditTxtMakeYear);
        textName10 = (EditText) findViewById(R.id.EditTxtEngineType);
        textName12 = (EditText) findViewById(R.id.EditTxtMileageRange);
        textName15 = (EditText) findViewById(R.id.EditTxtRunMileage);



        Switch switch_oilfilter = findViewById(R.id.EditTxtOilFilter);
        Switch switch_engineoil = findViewById(R.id.EngineOil);
        Switch switch_washpD = findViewById(R.id.washPD);






        Switch switch_DAPF = findViewById(R.id.EditTxtDAPF);
        Switch switch_WAB = findViewById(R.id.EditTxtWAB);
        Switch switch_ACF = findViewById(R.id.EditTxtACF);
        Switch switch_coolent = findViewById(R.id.EditTxtCoolent);
        Switch switch_BreakFluid = findViewById(R.id.BrakeFluid);
        Switch switch_brakePads = findViewById(R.id.brakePads);
        Switch switch_FF = findViewById(R.id.EditTxtFF);
        Switch switch_Sparkplug = findViewById(R.id.Sparkplug);
        Switch switch_clutch = findViewById(R.id.clutch);
        Switch switch_BreakAndClutchOil = findViewById(R.id.BreakAndClutchOil);
        Switch switch_transmission_fluid = findViewById(R.id.Edittransmission_fluid);

//

        switch_oilfilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton1, boolean b1) {
                if(b1) {
                    oilFilter = 1;
                }else{
                    oilFilter = 0;
                }
            }
        });

        switch_engineoil.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton2, boolean b2) {
                if(b2) {
                    engine_oil = 1;
                }else{
                    engine_oil = 0;
                }
            }
        });

        switch_washpD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton3, boolean b3) {
                if(b3) {
                    wash_PD = 1;
                }else{
                    wash_PD = 0;
                }
            }
        });

        switch_DAPF.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton4, boolean b4) {
                if(b4) {
                    DA_PF = 1;
                }else{
                    DA_PF = 0;
                }
            }
        });

        switch_WAB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton5, boolean b5) {
                if(b5) {
                    WA_B = 1;
                }else{
                    WA_B = 0;
                }
            }
        });

        switch_ACF.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton6, boolean b6) {
                if(b6) {
                    AC_F = 1;
                }else{
                    AC_F = 0;
                }
            }
        });
        switch_coolent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton7, boolean b7) {
                if(b7) {
                    cool_ent = 1;
                }else{
                    cool_ent = 0;
                }
            }
        });
        switch_BreakFluid.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton8, boolean b8) {
                if(b8) {
                    Break_Fluid = 1;
                }else{
                    Break_Fluid = 0;
                }
            }
        });
        switch_brakePads.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton9, boolean b9) {
                if(b9) {
                    brake_Pads = 1;
                }else{
                    brake_Pads = 0;
                }
            }
        });
        switch_FF.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton10, boolean b10) {
                if(b10) {
                    F_F = 1;
                }else{
                    F_F = 0;
                }
            }
        });
        switch_Sparkplug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton11, boolean b11) {
                if(b11) {
                    SparkPlug = 1;
                }else{
                    SparkPlug = 0;
                }
            }
        });
        switch_clutch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton12, boolean b12) {
                if(b12) {
                    clut_ch = 1;
                }else{
                    clut_ch = 0;
                }
            }
        });
        switch_BreakAndClutchOil.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton13, boolean b13) {
                if(b13) {
                    Break_And_Clutch_Oil = 1;
                }else{
                    Break_And_Clutch_Oil = 0;
                }
            }
        });
        switch_transmission_fluid.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton14, boolean b14) {
                if(b14) {
                    transmission_fluid = 1;
                }else{
                    transmission_fluid = 0;
                }
            }
        });

//        togglebutton = (ToggleButton) findViewById(R.id.toggleButton);





        spinner6 = findViewById(R.id.spinner6);
        ArrayAdapter<CharSequence> adapter6 = ArrayAdapter.createFromResource(this,
                R.array.vehicleType, android.R.layout.simple_spinner_item);
        adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner6.setAdapter(adapter6);
        spinner6.setOnItemSelectedListener(this);

        spinner7 = findViewById(R.id.spinner7);
        ArrayAdapter<CharSequence> adapter7 = ArrayAdapter.createFromResource(this,
                R.array.vehicleBrand, android.R.layout.simple_spinner_item);
        adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner7.setAdapter(adapter7);
        spinner7.setOnItemSelectedListener(this);

        spinner8 = findViewById(R.id.spinner8);
        ArrayAdapter<CharSequence> adapter8 = ArrayAdapter.createFromResource(this,
                R.array.VehicleModel, android.R.layout.simple_spinner_item);
        adapter8.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner8.setAdapter(adapter8);
        spinner8.setOnItemSelectedListener(this);

        spinner9 = findViewById(R.id.spinner9);
        ArrayAdapter<CharSequence> adapter9 = ArrayAdapter.createFromResource(this,
                R.array.MakeYear, android.R.layout.simple_spinner_item);
        adapter9.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner9.setAdapter(adapter9);
        spinner9.setOnItemSelectedListener(this);

        spinner10 = findViewById(R.id.spinner10);
        ArrayAdapter<CharSequence> adapter10 = ArrayAdapter.createFromResource(this,
                R.array.EngineType, android.R.layout.simple_spinner_item);
        adapter10.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner10.setAdapter(adapter10);
        spinner10.setOnItemSelectedListener(this);

        spinner12 = findViewById(R.id.spinner12);
        ArrayAdapter<CharSequence> adapter12 = ArrayAdapter.createFromResource(this,
                R.array.MileageRange, android.R.layout.simple_spinner_item);
        adapter12.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner12.setAdapter(adapter12);
        spinner12.setOnItemSelectedListener(this);

        add = findViewById(R.id.btnSendForm);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (textName6 .getText().toString().isEmpty() || textName7.getText().toString().isEmpty() ||  textName8.getText().toString().isEmpty()||
                        textName9.getText().toString().isEmpty() ||
                        textName10.getText().toString().isEmpty() ||
                        textName12.getText().toString().isEmpty()) {

                    System.out.println("HY1");
                    Toast.makeText(PredictedMileage.this, "Please enter All the values", Toast.LENGTH_SHORT).show();

                    return;

                }else{

                    System.out.println("HY2");

                    String TypeD=textName6 .getText().toString();
                    String ModelD=textName8.getText().toString();
                    String make_yearD=textName10.getText().toString();
                    String BrandD=textName7.getText().toString();
                    String EngineTypeD=textName9.getText().toString();

                    String vehicleD=TypeD+"/"+ModelD+"/"+make_yearD+"/"+BrandD+"/"+EngineTypeD;


                    FileOutputStream outputStream;
                    try{
                        outputStream = openFileOutput("myfile1",MODE_PRIVATE);
                        outputStream.write(vehicleD.getBytes());
                        outputStream.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    postData(textName6.getText().toString(), textName7.getText().toString(),textName8.getText().toString(),textName10.getText().toString(),textName9.getText().toString(),textName12.getText().toString(),"Chennai",
                            textName15.getText().toString(),oilFilter,wash_PD,WA_B,F_F,Break_Fluid,transmission_fluid,clut_ch,engine_oil,
                            DA_PF,AC_F,SparkPlug,Break_And_Clutch_Oil,brake_Pads,cool_ent);


                            Toast.makeText(PredictedMileage.this, "You form is sucessfull", Toast.LENGTH_SHORT).show();
                }


            }
        });

        clear = findViewById(R.id.btnclearForm);

        clear.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                spinner6.setSelection(0,true);
                spinner7.setSelection(0,true);
                spinner8.setSelection(0,true);
                spinner9.setSelection(0,true);
                spinner10.setSelection(0,true);
                spinner12.setSelection(0,true);
                textName15.getText().clear();
            }
        });



    }

    private void postData(String vehicle_type,
                          String brand,
                          String model,
                          String engine_type,
                          String make_year,
                          String mileage_range,
                          String region,
                          String mileage,
                          int oil_filter,
                          int washer_plug_drain,
                          int Wheel_alignment_and_balance,
                          int Fuel_filter,
                          int Break_fluid,
                          int Transmission_Fluid,
                          int clutch,
                          int Engine_Oil,
                          int Dust_and_pollen_filter,
                          int Air_clean_filter,
                          int Spark_plug,
                          int Break_and_Clutch_oil,
                          int Break_Pads,
                          int Coolant
                                    ){



        // builder and passing our base url
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.5:8000/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
//        Gson gson = new Gson();

        RetrofitAPI retrofitAPI = retrofit.create(RetrofitAPI.class);


        // passing data from our text fields to our modal class.
        DataModal modal = new DataModal(vehicle_type, brand,model,engine_type,make_year,mileage_range,mileage,oil_filter,
                washer_plug_drain,Wheel_alignment_and_balance,Fuel_filter,Break_fluid,Transmission_Fluid,clutch,Engine_Oil,
                Dust_and_pollen_filter,Air_clean_filter,Spark_plug,Break_and_Clutch_oil,Break_Pads,Coolant);

        Call<DataModal> call = retrofitAPI.createPost(modal);



        call.enqueue(new Callback<DataModal>() {

            @Override
            public void onResponse(Call<DataModal> call, Response<DataModal> response) {

                Toast.makeText(PredictedMileage.this, "Data added", Toast.LENGTH_SHORT).show();
                DataModal responseFromAPI1 = response.body();
                TextView textName19 = (TextView) findViewById(R.id.textView19);

                textName19.setText(responseFromAPI1.getPredictedVal()+"km");
                cal2=Math.round(responseFromAPI1.getPredictedVal());
                System.out.println(responseFromAPI1.getPredictedVal());





            }

            @Override
            public void onFailure(Call<DataModal> call, Throwable t) {

                Toast.makeText(PredictedMileage.this, "Data can't added to API", Toast.LENGTH_SHORT).show();
                System.out.println(t.getMessage());

            }
        });



    }



    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        Spinner spinner = (Spinner) parent;
        if(spinner.getId() == R.id.spinner6)
        {
            text6 = parent.getItemAtPosition(position).toString();
            textName6.setText(text6);
        }
        else if(spinner.getId() == R.id.spinner7)
        {
            String text7 = parent.getItemAtPosition(position).toString();
            textName7.setText(text7);

        }else if(spinner.getId() == R.id.spinner8)
        {
            String text8 = parent.getItemAtPosition(position).toString();
            textName8.setText(text8);

        }else if(spinner.getId() == R.id.spinner9)
        {
            String text9 = parent.getItemAtPosition(position).toString();
            textName9.setText(text9);

        }else if(spinner.getId() == R.id.spinner10)
        {
            String text10 = parent.getItemAtPosition(position).toString();
            textName10.setText(text10);
      }
        else if(spinner.getId() == R.id.spinner12)
        {
            String text12 = parent.getItemAtPosition(position).toString();
            textName12.setText(text12);

        }
    }




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

    }


}