package com.sliit.smartserviceassistant;

import android.content.Intent;
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.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

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

public class predicted_suggestions extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    TextView textName6;
    TextView textName7;
    TextView textName8;
    TextView textName9;
    TextView textName10;
    TextView textName11;
    TextView textName12;
    EditText textName14;
    EditText textName15;
    public static int 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;
    public static int checking1;
    public static int checking2;
    Spinner spinner6,spinner7,spinner8,spinner9,spinner10,spinner12;

    Button clear,add;

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

        textName6 = (TextView) findViewById(R.id.EditTxtVehicleType);
        textName7 = (TextView) findViewById(R.id.EditTxtVehicleBrand);
        textName8 = (TextView) findViewById(R.id.EditTxtVehicleModel);
        textName9 = (TextView) findViewById(R.id.EditTxtMakeYear);
        textName10 = (TextView) findViewById(R.id.EditTxtEngineType);
        textName12 = (TextView) findViewById(R.id.EditTxtMileageRange);
        textName14=(EditText) findViewById(R.id.EditTxtMileage);
        textName15 = (EditText) findViewById(R.id.EditTxtpredictedMileage);
//
//
        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.btnSend);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                System.out.println("HY1");

                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(predicted_suggestions.this, "Please enter All the values", Toast.LENGTH_SHORT).show();

                    return;

                }else{
                    System.out.println("HY2");
                    float f=Float.parseFloat(textName15.getText().toString());
            postData12(textName6.getText().toString(), textName7.getText().toString(),textName8.getText().toString(),textName10.getText().toString(),textName9.getText().toString(),textName12.getText().toString(),"Chennai",
                           textName14.getText().toString(),f);


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

                }


            }
        });

        clear = findViewById(R.id.btnclear);

        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);
                textName14.getText().clear();
                textName15.getText().clear();
            }
        });
//

    }

    private void postData12(String vehicle_type,
                          String brand,
                          String model,
                          String engine_type,
                          String make_year,
                          String mileage_range,
                          String region,
                          String mileage,
                          float predicted_data

    ){

        Retrofit retrofit2 = new Retrofit.Builder()
                .baseUrl("http://192.168.1.5:8000/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();


//        "vehicle_type": "Car",
//                "brand": "Toyota",
//                "model": "amaze",
//                "make_year": "Diesel",
//                "engine_type": "2016",
//                "mileage_range": "60000",
//                "mileage": "856"
        RetrofitAPI2 retrofitAPI2 = retrofit2.create(RetrofitAPI2.class);

        DataModal2 modal2 = new DataModal2(vehicle_type,brand,model,make_year,engine_type,mileage_range,mileage,predicted_data);




        Call<DataModal2> call2 = retrofitAPI2.createPost2(modal2);

        call2.enqueue(new Callback<DataModal2>() {

            @Override
            public void onResponse(Call<DataModal2> call2, Response<DataModal2> response2) {

                Toast.makeText(predicted_suggestions.this, "Data added", Toast.LENGTH_SHORT).show();
                List<Integer> predicted_value_ss=new ArrayList<Integer>();
                predicted_value_ss=response2.body().getPredictedVals();
                System.out.println(predicted_value_ss);

//                Maintainces classone1 = new Maintainces();
//                checking2=classone1.checking2;


                oil_filter=predicted_value_ss.get(0);
                washer_plug_drain=predicted_value_ss.get(1);
                Wheel_alignment_and_balance=predicted_value_ss.get(2);
                Fuel_filter=predicted_value_ss.get(3);
                Break_fluid=predicted_value_ss.get(4);
                Transmission_Fluid=predicted_value_ss.get(5);
                clutch=predicted_value_ss.get(6);
                Engine_Oil=predicted_value_ss.get(7);
                Dust_and_pollen_filter=predicted_value_ss.get(8);
                Air_clean_filter=predicted_value_ss.get(9);
                Spark_plug=predicted_value_ss.get(10);
                Break_and_Clutch_oil=predicted_value_ss.get(11);
                Break_Pads=predicted_value_ss.get(12);
                Coolant=predicted_value_ss.get(13);
                System.out.println(predicted_value_ss);

                startActivityForResult(new Intent(predicted_suggestions.this, SuggestionAnswer.class), 0);

//                System.out.println(predicted_value_ss.get(0));
//                System.out.println(predicted_value_ss.get(1));
//                System.out.println(predicted_value_ss.get(2));
//                System.out.println(predicted_value_ss.get(3));
//                System.out.println(predicted_value_ss.get(4));
//                System.out.println(predicted_value_ss.get(5));
//                System.out.println(predicted_value_ss.get(6));
//                System.out.println(predicted_value_ss.get(7));
//                System.out.println(predicted_value_ss.get(8));
//                System.out.println(predicted_value_ss.get(9));
//                System.out.println(predicted_value_ss.get(10));
//                System.out.println(predicted_value_ss.get(11));
//                System.out.println(predicted_value_ss.get(12));
//                System.out.println(predicted_value_ss.get(13));



            }

            @Override
            public void onFailure(Call<DataModal2> call2, Throwable t) {

                Toast.makeText(predicted_suggestions.this, "Data can't added", 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) {

    }


}


