package com.cygnusa.hyperm;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.cygnusa.hyperm.dbadapters.CustomerEnquiryDataAdapters;
import com.cygnusa.hyperm.swipe.EnquirySwipeSelector;
import com.cygnusa.hyperm.swipe.OnSwipeItemSelectedListener;
import com.cygnusa.hyperm.swipe.SwipeItem;
import com.cygnusa.hyperm.util.ApiService;
import com.cygnusa.hyperm.util.CommonMethods;
import com.cygnusa.hyperm.util.GlobalData;
import com.cygnusa.hyperm.util.MainApplication;
import com.cygnusa.hyperm.util.StorePreference;
import com.cygnusa.hyperm.util.TransparentProgressDialog;
import com.cygnusa.hyperm.wrappers.CommonResponse;
import com.cygnusa.hyperm.wrappers.CustomerEnquiryModel;
import com.cygnusa.hyperm.wrappers.ErrorResponse;
import com.cygnusa.hyperm.wrappers.GetAllExploreModel;
import com.cygnusa.hyperm.wrappers.GetAllPersonalizationModel;
import com.cygnusa.hyperm.wrappers.SendEnquiryDataModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HyperMEnquiryForm extends AppCompatActivity implements View.OnClickListener, OnSwipeItemSelectedListener {
    EnquirySwipeSelector swipeSelector;
    ProgressBar progressBar;
    StorePreference storePreference;
    private Spinner spnVariant;
    EditText edtCustName, edtMobNumber, edtEmailAddress;
    ImageView imgCheck, backButton, img_rating;
    Button btnSend, btn_tv_send;
    String VariantId, selectedModel;
    int position = 1;
    ApiService restService;
    private boolean isChecked = true;
    LinearLayout llout_buttons, llout_lay;
    private GetAllExploreModel exploreModel;
    ArrayAdapter<String> spinnerArrayAdapter = null;
    ImageView img_close;
    private GetAllPersonalizationModel personalizationModel;
    LinearLayout lloutCheckTerms;
    List<String> stringList = new ArrayList<>();
    String modelId = "";
    private HashMap<String, String> enquiryDetailsMap;
    private TransparentProgressDialog progressDialog;
    private TextView txtBikeModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enquiry_form);
        swipeSelector = findViewById(R.id.swipeSelector);
        progressBar = findViewById(R.id.progress);
        progressBar.setVisibility(View.GONE);
        storePreference = new StorePreference(this);
        backButton = findViewById(R.id.backButton);
        txtBikeModel = findViewById(R.id.bike_model);
        backButton.setOnClickListener(this);
        llout_lay = findViewById(R.id.linear);
        llout_buttons = findViewById(R.id.llout_buttons);
        llout_buttons.setOnClickListener(this);
        img_rating = findViewById(R.id.img_rating);
        img_rating.setOnClickListener(this);
        img_close = findViewById(R.id.img_close);
        img_close.setOnClickListener(this);
        btn_tv_send = findViewById(R.id.btn_tv_send);
        btn_tv_send.setOnClickListener(this);
        lloutCheckTerms = findViewById(R.id.lloutCheckTerms);
        personalizationModel = new GetAllPersonalizationModel();
        exploreModel = new GetAllExploreModel();
        spnVariant = findViewById(R.id.spnVariant);
        restService = ((MainApplication) getApplication()).getClient();
        edtCustName = findViewById(R.id.edtCustName);
        edtMobNumber = findViewById(R.id.edtMobNumer);
        edtEmailAddress = findViewById(R.id.edtEmailAddress);
        imgCheck = findViewById(R.id.imgCheck);
        btnSend = findViewById(R.id.btn_send);
        progressDialog = new TransparentProgressDialog(HyperMEnquiryForm.this);
        btnSend.setOnClickListener(this);
        imgCheck.setOnClickListener(this);
        modelId = storePreference.getStringValue(GlobalData.SELECTED_MODEL);
        swipeSelector.setOnItemSelectedListener(this);

        spnVariant.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                VariantId = personalizationModel.getPersonalizationDataModelList().get(position).getId();
            }

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

            }
        });

        if (CommonMethods.isNetworkAvailable(this)) {
            getAllExploreImageDataList();
            // getAllPersonalizationDetails("1");
        } else {
            checkLocalData();
            getAllExploreLocalDataModel();
        }
        if (GlobalData.user.getData().getRole().equalsIgnoreCase("16")) {
            llout_buttons.setVisibility(View.VISIBLE);
            btn_tv_send.setVisibility(View.VISIBLE);
            btnSend.setVisibility(View.GONE);
            lloutCheckTerms.setVisibility(View.GONE);
            edtCustName.setText(storePreference.getStringValue(GlobalData.DETAIL_NAME));
            edtEmailAddress.setText(storePreference.getStringValue(GlobalData.DETAIL_EMAIL));
            edtMobNumber.setText(storePreference.getStringValue(GlobalData.DETAIL_MOBILE));
//            edtMobNumber.setShowSoftInputOnFocus(false);
//            edtEmailAddress.setShowSoftInputOnFocus(false);
//            edtCustName.setShowSoftInputOnFocus(false);
        } else {
            llout_buttons.setVisibility(View.GONE);
            llout_buttons.setVisibility(View.GONE);
            btn_tv_send.setVisibility(View.GONE);
            btnSend.setVisibility(View.VISIBLE);
            lloutCheckTerms.setVisibility(View.VISIBLE);
        }

        if (CommonMethods.isNetworkAvailable(this)) {
            sendOfflineEnquiryDataToServer();
        }
        int selectVal = 0;
        try {
            selectVal = Integer.parseInt(storePreference.getStringValue(GlobalData.SELECTED_MODEL));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        getAllPersonalizationDetails(String.valueOf(selectVal));
    }

    private void getAllExploreLocalDataModel() {
        SharedPreferences pref = Objects.requireNonNull(this).getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
        if (pref.getString("ENQUIRY_FORM_EXPLORE_IMAGE" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null) != null) {
            Gson gson = new Gson();
            exploreModel = gson.fromJson(
                    pref.getString("ENQUIRY_FORM_EXPLORE_IMAGE" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null),
                    new TypeToken<GetAllExploreModel>() {
                    }.getType());
            assert exploreModel != null;
            if (exploreModel.getData() != null && exploreModel.getData().size() > 0) {
                SwipeItem[] swipeItems = new SwipeItem[exploreModel.getData().size()];
                for (int i = 0; i < exploreModel.getData().size(); i++) {
                    SwipeItem item = new SwipeItem("test", exploreModel.getData().get(i).getModelEnquiryUrl(), exploreModel.getData().get(i).getModelId());
                    swipeItems[i] = item;
                }
                swipeSelector.setItems(swipeItems);
            }

        }
    }

    private void checkLocalData() {
        SharedPreferences pref = Objects.requireNonNull(this).getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
        if (pref.getString("ENQUIRY_FORM_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null) != null) {
            Gson gson = new Gson();
            personalizationModel = gson.fromJson(
                    pref.getString("ENQUIRY_FORM_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null),
                    new TypeToken<GetAllPersonalizationModel>() {
                    }.getType());
            stringList = new ArrayList<>();
            for (int i = 0; i < personalizationModel.getPersonalizationDataModelList().size(); i++) {
                stringList.add(personalizationModel.getPersonalizationDataModelList().get(i).getTitle());
            }
            spinnerArrayAdapter = new ArrayAdapter<String>(
                    HyperMEnquiryForm.this, R.layout.spinner_item, stringList);
            spnVariant.setAdapter(spinnerArrayAdapter);
        } else {
            llout_lay.setVisibility(View.INVISIBLE);
            txtBikeModel.setVisibility(View.INVISIBLE);
            personalizationModel = new GetAllPersonalizationModel();
            stringList = new ArrayList<>();
            stringList.add("No Data");
            spinnerArrayAdapter = new ArrayAdapter<String>(
                    HyperMEnquiryForm.this, R.layout.spinner_item, stringList);
            spnVariant.setAdapter(spinnerArrayAdapter);

        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void getAllPersonalizationDetails(String s) {
        progressBar.setVisibility(View.VISIBLE);
        restService.getAllPersonalizationModel(s).enqueue(new Callback<GetAllPersonalizationModel>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onResponse(@NonNull Call<GetAllPersonalizationModel> call, @NonNull Response<GetAllPersonalizationModel> response) {
                progressBar.setVisibility(View.GONE);
                SharedPreferences pref = HyperMEnquiryForm.this.getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                personalizationModel = new GetAllPersonalizationModel();
                if (response.body() != null && response.body().getStatus() == 200) {
                    if (response.body().getPersonalizationDataModelList().size() > 0) {
                        llout_lay.setVisibility(View.VISIBLE);
                        txtBikeModel.setVisibility(View.VISIBLE);
                        personalizationModel = response.body();
                        if (personalizationModel.getPersonalizationDataModelList() != null && personalizationModel.getPersonalizationDataModelList().size() > 0)
                            VariantId = personalizationModel.getPersonalizationDataModelList().get(0).getId();
                        Gson gson = new Gson();
                        JsonElement jsonElement = gson.toJsonTree(
                                personalizationModel, new TypeToken<GetAllPersonalizationModel>() {
                                }.getType());
                        editor.putString("ENQUIRY_FORM_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL),
                                jsonElement.toString()).apply();
                        stringList = new ArrayList<>();
                        for (int i = 0; i < personalizationModel.getPersonalizationDataModelList().size(); i++) {
                            stringList.add(personalizationModel.getPersonalizationDataModelList().get(i).getTitle());
                        }
                        spinnerArrayAdapter = new ArrayAdapter<String>(
                                HyperMEnquiryForm.this, R.layout.spinner_item, stringList);
                        spnVariant.setAdapter(spinnerArrayAdapter);

                    } else {
                        checkLocalData();
                    }
                } else if (response.body() != null && response.body().getStatus() == 401) {
                    GlobalData.canLogout = true;
                    finish();
                } else if (response.body() != null && response.body().getStatus() == 406) {
                    checkLocalData();
                } else if (response.errorBody() != null) {
                    Gson gson = new Gson();
                    ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
                    GlobalData.showToast(HyperMEnquiryForm.this, message.getMessage(), Toast.LENGTH_SHORT).show();
                    if (message.getStatus() == 401) {
                        GlobalData.canLogout = true;
                        finish();
                    }
                    checkLocalData();
                } else {
                    checkLocalData();
                }
            }

            @Override
            public void onFailure(Call<GetAllPersonalizationModel> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                if (t.getMessage() != null) {
                    Log.e("Server Error", t.getMessage());
                    checkLocalData();
                }
            }
        });
    }

    private void getAllExploreImageDataList() {
        restService.getAllExploreImage().enqueue(new Callback<GetAllExploreModel>() {
            @Override
            public void onResponse(@NonNull Call<GetAllExploreModel> call, @NonNull Response<GetAllExploreModel> response) {
                if (response.body() != null) {
                    SharedPreferences pref = HyperMEnquiryForm.this.getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = pref.edit();
                    if (response.body().getData().size() > 0) {
                        exploreModel = response.body();
                        Gson gson = new Gson();
                        JsonElement jsonElement = gson.toJsonTree(
                                exploreModel, new TypeToken<GetAllExploreModel>() {
                                }.getType());
                        editor.putString("ENQUIRY_FORM_EXPLORE_IMAGE" + storePreference.getStringValue(GlobalData.SELECTED_MODEL),
                                jsonElement.toString()).apply();
                        SwipeItem[] swipeItems = new SwipeItem[response.body().getData().size()];
                        for (int i = 0; i < response.body().getData().size(); i++) {
                            SwipeItem item = new SwipeItem("test", response.body().getData().get(i).getModel_brochure_image_url(), response.body().getData().get(i).getModelId());
                            swipeItems[i] = item;
                        }
                        getAllPersonalizationDetails(response.body().getData().get(0).getModelId());
                        swipeSelector.setItems(swipeItems);
                    } else {
                        getAllExploreLocalDataModel();
                    }
                } else if (response.errorBody() != null) {
                    Gson gson = new Gson();
                    ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
                    GlobalData.showToast(HyperMEnquiryForm.this, message.getMessage(), Toast.LENGTH_SHORT).show();
                    if (message.getStatus() == 401) {
                        GlobalData.canLogout = true;
                        startActivity(new Intent(getBaseContext(), LoginActivity.class));
                        finish();
                    }
                    getAllExploreLocalDataModel();
                } else {
                    getAllExploreLocalDataModel();
                }
            }

            @Override
            public void onFailure(Call<GetAllExploreModel> call, Throwable t) {
                getAllExploreLocalDataModel();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backButton:
                onBackPressed();
                break;
            case R.id.img_close:
                openCloseAlertBox();
                break;
            case R.id.img_rating:
                startActivity(new Intent(HyperMEnquiryForm.this, ProductFeedbackFormActivity.class));
                break;
            case R.id.imgCheck:
                if (!isChecked) {
                    imgCheck.setImageResource(android.R.drawable.checkbox_on_background);
                    isChecked = true;
                } else {
                    imgCheck.setImageResource(android.R.drawable.checkbox_off_background);
                    isChecked = false;
                }
                break;
            case R.id.btn_send:
                checkFieldEmpty();
                break;
            case R.id.btn_tv_send:
                checkFieldEmpty();
                break;
            default:
        }
    }

    private void openCloseAlertBox() {
        final Dialog aDialog = new Dialog(this);
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_alert_close);
        aDialog.setCancelable(true);
        TextView aAlertMessage = aDialog.findViewById(R.id.txt_alert_message);
        aAlertMessage.setText(String.format("Dear %s", storePreference.getStringValue(GlobalData.PRODUCT_NAME)));
        aDialog.show();
        Button btnYes = aDialog.findViewById(R.id.btn_yes);
        Button btnNo = aDialog.findViewById(R.id.btn_no);
        btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                openFeedbackAlertBox();
                aDialog.dismiss();
            }
        });
        btnNo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                aDialog.dismiss();
            }

        });


    }

    private void openFeedbackAlertBox() {
        final Dialog aDialog = new Dialog(this);
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_alert_feedback);
        aDialog.setCancelable(true);
        TextView aAlertMessage = aDialog.findViewById(R.id.txt_alert_message);
        aAlertMessage.setText(String.format("Dear %s", storePreference.getStringValue(GlobalData.PRODUCT_NAME)));
        aDialog.show();
        TextView txtSkip;
        ImageView imgClose;
        imgClose = aDialog.findViewById(R.id.img_close);
        txtSkip = aDialog.findViewById(R.id.txtSkip);
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                aDialog.dismiss();
            }
        });
        txtSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                aDialog.dismiss();
                startActivity(new Intent(HyperMEnquiryForm.this, ProductExplorerActivity.class));
                finish();
            }
        });
        Button btnYes = aDialog.findViewById(R.id.btn_yes);
        btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                aDialog.dismiss();
                startActivity(new Intent(HyperMEnquiryForm.this, ProductFeedbackFormActivity.class));
                finish();

            }
        });
    }

    private void hideKeyboard(Activity activity) {
        InputMethodManager inputMethodManager =
                (InputMethodManager) activity.getSystemService(
                        Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(
                Objects.requireNonNull(activity.getCurrentFocus()).getWindowToken(), 0);
    }

    @SuppressLint("SetTextI18n")
    private void openAlertBox() {
        final Dialog aDialog = new Dialog(this);
        aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        aDialog.setContentView(R.layout.popup_alert_box);
        aDialog.setCancelable(true);
        TextView aAlertMessage = aDialog.findViewById(R.id.txt_alert_message);
        TextView description = aDialog.findViewById(R.id.description);

        if (!isChecked) {
            description.setText("Thank you for showing your interest in HYPER-M.");
        }

        aAlertMessage.setText(String.format("Dear %s", edtCustName.getText().toString().trim()));
        aDialog.show();
        Button aButton = aDialog.findViewById(R.id.btn_alert);
        aButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                aDialog.dismiss();
                onBackPressed();
            }

        });
    }

    public boolean validateEmailID(String str) {
        return str.contains("@") && str.lastIndexOf(".") != -1;
    }

    private void checkFieldEmpty() {
        if (edtCustName.getText().toString().equalsIgnoreCase("")) {
            GlobalData.showToast(this, "Enter name", Toast.LENGTH_LONG).show();
        } else if (edtMobNumber.getText().toString().equalsIgnoreCase("")) {
            GlobalData.showToast(this, "Enter mobile number", Toast.LENGTH_LONG).show();
        } else if (edtEmailAddress.getText().toString().equalsIgnoreCase("")) {
            GlobalData.showToast(this, "Enter E-mail address", Toast.LENGTH_LONG).show();
        } else if (edtMobNumber.getText().length() < 10) {
            GlobalData.showToast(this, "Enter your valid mobile number.", Toast.LENGTH_LONG).show();
        } else if (!validateEmailID(edtEmailAddress.getText().toString())) {
            GlobalData.showToast(this, "Please enter your valid email id", Toast.LENGTH_LONG).show();
        } else {

            enquiryDetailsMap = new HashMap<>();
            enquiryDetailsMap.put("current_time", storePreference.getStringValue(GlobalData.CURRENTTIME));
            enquiryDetailsMap.put("variant_id", VariantId);
            enquiryDetailsMap.put("name", edtCustName.getText().toString());
            enquiryDetailsMap.put("model_id", modelId);
            enquiryDetailsMap.put("mobile", edtMobNumber.getText().toString());
            enquiryDetailsMap.put("is_brochure", isChecked ? "1" : "0");
            enquiryDetailsMap.put("email", edtEmailAddress.getText().toString());
            if (CommonMethods.isNetworkAvailable(this)) {
                sendOfflineEnquiryDataToServer();
                sendAllCustomerEnquiry();
            } else {
                insetDetailsToLocalDB();
            }
        }
    }

    private void sendOfflineEnquiryDataToServer() {
        CustomerEnquiryModel detailsModel = new CustomerEnquiryModel();
        CustomerEnquiryDataAdapters customerEnquiryDataAdapters = new CustomerEnquiryDataAdapters(HyperMEnquiryForm.this);
        List<SendEnquiryDataModel> getAllOfflineData = customerEnquiryDataAdapters.getAllEnquiryFromTable();
        List<SendEnquiryDataModel> allEnquiryData = new ArrayList<>();
        if (getAllOfflineData != null && getAllOfflineData.size() > 0) {
            detailsModel.setEmp_code(GlobalData.user.getData().getEmp_code());
            detailsModel.setSmpin(GlobalData.user.getData().getSmpin());
            for (int i = 0; i < getAllOfflineData.size(); i++) {
                SendEnquiryDataModel customerData = new SendEnquiryDataModel();
                customerData.setModelId(getAllOfflineData.get(i).getModelId());
                customerData.setIsBrochure(getAllOfflineData.get(i).getIsBrochure());
                customerData.setEmail(getAllOfflineData.get(i).getEmail());
                customerData.setName(getAllOfflineData.get(i).getName());
                customerData.setVariantId(getAllOfflineData.get(i).getVariantId());
                customerData.setMobile(getAllOfflineData.get(i).getMobile());
                customerData.setUid(getAllOfflineData.get(i).getUid());
                allEnquiryData.add(customerData);
            }
            detailsModel.setCust_enquiry(allEnquiryData);
            if (detailsModel.getCust_enquiry() != null && detailsModel.getCust_enquiry().size() > 0) {
                restService.sendAllCustomerEnquiry(detailsModel).enqueue(new Callback<CommonResponse>() {
                    @Override
                    public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                        progressBar.setVisibility(View.GONE);
                        if (response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                openAlertBox();
                                edtCustName.setText("");
                                edtEmailAddress.setText("");
                                edtMobNumber.setText("");
                            } else {
                                GlobalData.showToast(HyperMEnquiryForm.this, response.body().getMessage(), Toast.LENGTH_LONG).show();
                            }
                        } else {
                            if (response.errorBody() != null) {
                                CommonMethods.errorShow(HyperMEnquiryForm.this, response.errorBody());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResponse> call, Throwable t) {
                        progressBar.setVisibility(View.GONE);
                    }
                });
            }
        }
    }

    private void insetDetailsToLocalDB() {
        CustomerEnquiryDataAdapters customerEnquiryDataAdapters = new CustomerEnquiryDataAdapters(HyperMEnquiryForm.this);
        if (!customerEnquiryDataAdapters.checkRecordExists(edtMobNumber.getText().toString())) {
            customerEnquiryDataAdapters.insertEnquiryDetails(enquiryDetailsMap);
            clearField();
        } else {
            clearField();
        }
    }

    private void clearField() {
        edtCustName.setText("");
        edtEmailAddress.setText("");
        edtMobNumber.setText("");
    }

    private void sendAllCustomerEnquiry() {
        progressDialog.show();
        restService.sendAllCustomerEnquiry(buildCustConfigData()).enqueue(new Callback<CommonResponse>() {
            @Override
            public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                if (response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        openAlertBox();
                        edtCustName.setText("");
                        edtEmailAddress.setText("");
                        edtMobNumber.setText("");
                    } else {
                        GlobalData.showToast(HyperMEnquiryForm.this, response.body().getMessage(), Toast.LENGTH_LONG).show();
                    }
                } else {
                    if (response.errorBody() != null) {
                        CommonMethods.errorShow(HyperMEnquiryForm.this, response.errorBody());
                    }
                }
            }

            @Override
            public void onFailure(Call<CommonResponse> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
            }
        });
    }

    private CustomerEnquiryModel buildCustConfigData() {
        CustomerEnquiryModel enquiryModel = new CustomerEnquiryModel();
        List<SendEnquiryDataModel> enquiryDataModel = new ArrayList<>();
        enquiryModel.setEmp_code(GlobalData.user.getData().getEmp_code());
        enquiryModel.setSmpin(GlobalData.user.getData().getSmpin());
        SendEnquiryDataModel dataModel = new SendEnquiryDataModel();
        dataModel.setEmail(edtEmailAddress.getText().toString());
        dataModel.setMobile(edtMobNumber.getText().toString());
        dataModel.setName(edtCustName.getText().toString());
        dataModel.setVariantId(VariantId);
        dataModel.setIsBrochure(isChecked ? "1" : "0");
        dataModel.setModelId(Integer.valueOf(modelId));
        dataModel.setUid(storePreference.getStringValue(GlobalData.CURRENTTIME));
        enquiryDataModel.add(dataModel);
        enquiryModel.setCust_enquiry(enquiryDataModel);
        return enquiryModel;
    }

    @Override
    public void onItemSelected(SwipeItem item) {
        selectedModel = item.getDescription();
        modelId = item.getDescription();
        storePreference.setString(GlobalData.SELECTED_MODEL, selectedModel);
        position = Integer.parseInt(item.getDescription());
        getAllPersonalizationDetails(String.valueOf(position));
    }
}
