package com.cygnusa.hyperm.fragments;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
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.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.cygnusa.hyperm.LoginActivity;
import com.cygnusa.hyperm.ProductFeedbackFormActivity;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.HyperMEnquiryForm;
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.wrappers.ErrorResponse;
import com.cygnusa.hyperm.wrappers.GetAllAccessoriesBikersRidersModel;
import com.cygnusa.hyperm.wrappers.GetAllAccessoriesByCategoryModel;
import com.cygnusa.hyperm.wrappers.GetAllPersonalizationModel;
import com.cygnusa.hyperm.wrappers.PersonalizationDataModel;

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

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

import static com.cygnusa.hyperm.MainActivity.img_broucher;
import static com.cygnusa.hyperm.MainActivity.img_rating;
import static com.cygnusa.hyperm.MainActivity.layout1;

public class HypermPersonalizationFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
    public ImageView imagCarNew, imagCarNew2;
    LinearLayout linearLayoutColor, linearLayoutColorParent, linear_wheel, linearImage, lloutVariantSpn;
    int lastSelectedImage = -1;
    int variantClickPosition = 0;
    int colorClickPosition = 0, chooseId = 0, chooseVariantId = 0;
    ArrayAdapter<String> spinnerArrayAdapter = null;
    ArrayAdapter<String> spinnerVariantAdapter = null;
    int layoutLength = 0, variantLength = 0, ridersAccessoriesLength = 0;
    ApiService restService;
    List<PersonalizationDataModel> MODELS;
    GetAllAccessoriesBikersRidersModel BIKERS_MODEL;
    RelativeLayout accessoriesButtons;
    ArrayList<String> stringList, variantList;
    ImageView imgLeft, imgRight;
    View rootView;
    boolean isBiker;
    private StorePreference storePreference;
    private ProgressBar progressBar;
    private ImageView imageView;
    private TextView headerTextView;
    private ImageView imgNoData;
    private RelativeLayout lloutWhole;
    private Button btnColor, btnAccessories, btn_bikers, btn_Riders;
    private Spinner spinnerRiderId, spinnerVariant;
    private LinearLayout lloutSpn, lloutDesc;
    private String accessId = "0";
    private TextView txtTitle, txtDEsc, txtCode, txtPrice;
    private LinearLayout bottomVariant;
    private HorizontalScrollView horizontalScrollView;

    public HypermPersonalizationFragment() {
        // Required empty public constructor
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.hyperm_fragment_personalization, container, false);
        imagCarNew = rootView.findViewById(R.id.imageCar1);
        imagCarNew2 = rootView.findViewById(R.id.imageCar2);
        linearImage = rootView.findViewById(R.id.linearimage);
        storePreference = new StorePreference(getActivity());
        imageView = rootView.findViewById(R.id.logo);
        imageView.setOnClickListener(this);
        bottomVariant = rootView.findViewById(R.id.bottomVariant);
        bottomVariant.setVisibility(View.GONE);
        horizontalScrollView = rootView.findViewById(R.id.horizontal_scroll);
        linearLayoutColor = rootView.findViewById(R.id.linear_color);
        linear_wheel = rootView.findViewById(R.id.linear_wheel);
        linear_wheel.setVisibility(View.GONE);
        progressBar = rootView.findViewById(R.id.progress);
        headerTextView = rootView.findViewById(R.id.textView);
        progressBar.setVisibility(View.GONE);
        imgNoData = rootView.findViewById(R.id.noDataFound);
        imgNoData.setVisibility(View.GONE);
        lloutWhole = rootView.findViewById(R.id.whole_layout);
        btnAccessories = rootView.findViewById(R.id.btn_accessories);
        btnColor = rootView.findViewById(R.id.btn_color);
        btnColor.setOnClickListener(this);
        accessoriesButtons = rootView.findViewById(R.id.accessories_buttons);
        accessoriesButtons.setVisibility(View.GONE);
        btn_bikers = rootView.findViewById(R.id.btn_bikers);
        btn_Riders = rootView.findViewById(R.id.btn_riders);
        spinnerRiderId = rootView.findViewById(R.id.spnRiderId);
        spinnerRiderId.setVisibility(View.GONE);

        lloutVariantSpn = rootView.findViewById(R.id.lloutVariantSpn);
        spinnerVariant = rootView.findViewById(R.id.spnVarient);

        lloutVariantSpn.setVisibility(View.VISIBLE);
        spinnerVariant.setVisibility(View.VISIBLE);

        lloutDesc = rootView.findViewById(R.id.lloutDesc);
        lloutDesc.setVisibility(View.GONE);
        imgLeft = rootView.findViewById(R.id.imgLeft);
        imgLeft.setVisibility(View.INVISIBLE);
        imgRight = rootView.findViewById(R.id.imgRight);
        imgRight.setVisibility(View.INVISIBLE);
        horizontalScrollView.setSmoothScrollingEnabled(true);
        imgLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //horizontalScrollView.fullScroll(View.FOCUS_LEFT);
                horizontalScrollView.scrollTo(horizontalScrollView.getScrollX() - 100, horizontalScrollView.getScrollY());

            }
        });
        imgRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                horizontalScrollView.scrollTo(horizontalScrollView.getScrollX() + 100, horizontalScrollView.getScrollY());
            }
        });
        txtTitle = rootView.findViewById(R.id.txt_title);
        txtDEsc = rootView.findViewById(R.id.txt_desc);
        txtCode = rootView.findViewById(R.id.txt_code);
        txtPrice = rootView.findViewById(R.id.txt_price);
        spinnerRiderId.setOnItemSelectedListener(this);
        spinnerVariant.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                variantClickPosition = position;
                layoutLength = MODELS.get(position).getColor().size();
                setImageGlide(MODELS.get(position).getColor().get(0).getImageUrl(), imagCarNew2);
                setColorButtons(MODELS, variantClickPosition, colorClickPosition);
            }

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

            }
        });
        lloutSpn = rootView.findViewById(R.id.lloutSpn);
        lloutSpn.setVisibility(View.GONE);
        stringList = new ArrayList<>();
        btn_bikers.setOnClickListener(this);
        btn_Riders.setOnClickListener(this);
        btnAccessories.setOnClickListener(this);
        //txtName.setText(storePreference.getStringValue(GlobalData.SELECTED_MODEL_NAME));
        restService = ((MainApplication) Objects.requireNonNull(getActivity()).getApplication()).getClient();
        lastSelectedImage = -1;
        img_broucher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), HyperMEnquiryForm.class));
            }
        });

        img_rating.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ProductFeedbackFormActivity.class));
            }
        });

        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), HyperMEnquiryForm.class));
            }
        });
        if (CommonMethods.isNetworkAvailable(getActivity())) {
            getAllPersonalizationDetails();
        } else {
            checkLocalPersonalization();
        }
        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

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

    private void getAllProductBasedOnId() {
        progressBar.setVisibility(View.VISIBLE);
        restService.getAllAccessoriesUsingId(storePreference.getStringValue(GlobalData.SELECTED_MODEL)).enqueue(new Callback<GetAllAccessoriesBikersRidersModel>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(Call<GetAllAccessoriesBikersRidersModel> call, Response<GetAllAccessoriesBikersRidersModel> response) {
                progressBar.setVisibility(View.GONE);
                if (response.body() != null && response.body().getStatus() == 200) {
                    if (response.body().getGetAllAccessoriesModel() != null) {
                        chooseVariantId = 0;
                        if (response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels() != null
                                && response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size() > 0) {

                            setImageGlide(response.body().getGetAllAccessoriesModel()
                                    .getGetAllAccessoriesByCategoryModels().get(0).getImageUrl(), imagCarNew);

                            if (response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size() > 0) {
                                linear_wheel.setVisibility(View.VISIBLE);
                                bottomVariant.setVisibility(View.VISIBLE);
                                txtTitle.setText(response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getTitle());
                                txtDEsc.setText(response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getDescription());
                                txtCode.setText(response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getTitle());
                                if (response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getPrice() != null && response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getPrice().equalsIgnoreCase("0")) {
                                    txtPrice.setVisibility(View.GONE);
                                } else {
                                    //txtPrice.setVisibility(View.VISIBLE);
                                    //txtPrice.setText("INR " + response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getPrice() + " /-");
                                }
                                variantLength = response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size();
                                setWheelView(response.body().getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels());
                            } else {
                                lloutDesc.setVisibility(View.GONE);
                                linear_wheel.setVisibility(View.GONE);
                                bottomVariant.setVisibility(View.GONE);
                                linearImage.setBackgroundResource(R.drawable.border_grey);
                                imagCarNew.setScaleType(ImageView.ScaleType.CENTER);
                                Glide.with(Objects.requireNonNull(getActivity())).load(R.drawable.coming_soon).into(imagCarNew);
                            }

                        } else {
                            linear_wheel.setVisibility(View.GONE);
                            bottomVariant.setVisibility(View.GONE);
                            lloutDesc.setVisibility(View.GONE);
                            linearImage.setBackgroundResource(R.drawable.border_grey);
                            imagCarNew.setScaleType(ImageView.ScaleType.CENTER);
                            Glide.with(Objects.requireNonNull(getActivity())).load(R.drawable.coming_soon).into(imagCarNew);
                        }
                    } else {
                        lloutDesc.setVisibility(View.GONE);
                        linear_wheel.setVisibility(View.GONE);
                        bottomVariant.setVisibility(View.GONE);
                        linearImage.setBackgroundResource(R.drawable.border_grey);
                        imagCarNew.setScaleType(ImageView.ScaleType.CENTER);
                        Glide.with(Objects.requireNonNull(getActivity())).load(R.drawable.coming_soon).into(imagCarNew);
                    }
                }
            }

            @Override
            public void onFailure(Call<GetAllAccessoriesBikersRidersModel> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                lloutDesc.setVisibility(View.GONE);
                linear_wheel.setVisibility(View.GONE);
                bottomVariant.setVisibility(View.GONE);
                linearImage.setBackgroundResource(R.drawable.border_grey);
                imagCarNew.setScaleType(ImageView.ScaleType.CENTER);
                Glide.with(Objects.requireNonNull(getActivity())).load(R.drawable.coming_soon).into(imagCarNew);
            }
        });
    }

    private void getAllAccessoriesDetails() {

        progressBar.setVisibility(View.VISIBLE);
        restService.getAllAccessoriesRider().enqueue(new Callback<GetAllAccessoriesBikersRidersModel>() {
            @Override
            public void onResponse(Call<GetAllAccessoriesBikersRidersModel> call, Response<GetAllAccessoriesBikersRidersModel> response) {
                progressBar.setVisibility(View.GONE);

                SharedPreferences pref = Objects.requireNonNull(getActivity()).getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();

                if (response.body() != null && response.body().getStatus() == 200) {
                    BIKERS_MODEL = response.body();
                    if (BIKERS_MODEL != null && BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size() > 0) {
                        Gson gson = new Gson();
                        JsonElement jsonElement = gson.toJsonTree(
                                BIKERS_MODEL, new TypeToken<GetAllAccessoriesBikersRidersModel>() {
                                }.getType());
                        editor.putString("ACCESSORIES_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL),
                                jsonElement.toString()).apply();
                        stringList = new ArrayList<>();
                        if (BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList() != null && BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().size() > 0) {
                            for (int i = 0; i < BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().size(); i++) {
                                stringList.add(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().get(i).getCatTitle());
                            }
                            spinnerArrayAdapter = new ArrayAdapter<>(
                                    Objects.requireNonNull(getActivity()), R.layout.spinner_item, stringList);
                            spinnerRiderId.setAdapter(spinnerArrayAdapter);
                        }

                        if (BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList() != null && BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().size() > 0) {
                            Log.e("default Value", BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().get(0).getId());
                            accessId = BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().get(0).getId();

                        }

                        setImageGlide(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getImageUrl(), imagCarNew);

                        if (BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size() > 1) {
                            ridersAccessoriesLength = BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size();
                            setAccessoriesWheelView(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels());
                        }
                    } else {
                        GlobalData.showToast(getActivity(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    checkLocalAccessories();
                }
            }

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

    private void checkLocalAccessories() {
        SharedPreferences pref = Objects.requireNonNull(getActivity()).getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
        if (pref.getString("ACCESSORIES_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null) != null) {
            Gson gson = new Gson();
            BIKERS_MODEL = gson.fromJson(
                    pref.getString("ACCESSORIES_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null),
                    new TypeToken<GetAllAccessoriesBikersRidersModel>() {
                    }.getType());

            assert BIKERS_MODEL != null;
            setImageGlide(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(0).getImageUrl(), imagCarNew);
            if (BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size() > 1) {
                variantLength = BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size();
                setAccessoriesWheelView(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels());

            }
        } else {
            lloutWhole.setVisibility(View.GONE);
            imgNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getAllPersonalizationDetails() {
        progressBar.setVisibility(View.VISIBLE);
        restService.getAllPersonalizationModel(storePreference.getStringValue(GlobalData.SELECTED_MODEL)).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 = Objects.requireNonNull(getActivity()).getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                if (response.body() != null && response.body().getStatus() == 200) {
                    MODELS = response.body().getPersonalizationDataModelList();
                    if (MODELS != null && MODELS.size() > 0) {
                        headerTextView.setText(storePreference.getStringValue(GlobalData.SELECTED_MODEL_NAME) + " - " + MODELS.get(0).getColor().get(0).getColorTitle());
                        headerTextView.setSelected(true);
                        Gson gson = new Gson();
                        JsonElement jsonElement = gson.toJsonTree(
                                MODELS, new TypeToken<List<PersonalizationDataModel>>() {
                                }.getType());
                        editor.putString("PERSONALIZATION_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL),
                                jsonElement.toString()).apply();
                        layoutLength = MODELS.get(0).getColor().size();

                        variantList = new ArrayList<>();
                        for (int i = 0; i < MODELS.size(); i++) {
                            variantList.add(MODELS.get(i).getTitle());
                        }
                        spinnerVariantAdapter = new ArrayAdapter<>(
                                Objects.requireNonNull(getActivity()), R.layout.spinner_item, variantList);
                        spinnerVariant.setAdapter(spinnerVariantAdapter);


                        setImageGlide(MODELS.get(0).getColor().get(0).getImageUrl(), imagCarNew2);
                        setColorButtons(MODELS, variantClickPosition, colorClickPosition);
                    } else {
                        lloutWhole.setVisibility(View.GONE);
                        imgNoData.setVisibility(View.VISIBLE);
                    }
                } else if (response.body() != null && response.body().getStatus() == 401) {
                    GlobalData.canLogout = true;
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    Objects.requireNonNull(getActivity()).finish();
                } else if (response.body() != null && response.body().getStatus() == 406) {
                    checkLocalPersonalization();
                } else if (response.errorBody() != null) {
                    Gson gson = new Gson();
                    ErrorResponse message = gson.fromJson(response.errorBody().charStream(), ErrorResponse.class);
                    GlobalData.showToast(Objects.requireNonNull(getActivity()), message.getMessage(), Toast.LENGTH_SHORT).show();
                    if (message.getStatus() == 401) {
                        GlobalData.canLogout = true;
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                        Objects.requireNonNull(getActivity()).finish();
                    }
                    checkLocalPersonalization();
                } else {
                    checkLocalPersonalization();
                }

            }

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

    private void checkLocalPersonalization() {
        SharedPreferences pref = Objects.requireNonNull(getActivity()).getSharedPreferences("PRODUCT_MODULE", Context.MODE_PRIVATE);
        if (pref.getString("PERSONALIZATION_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null) != null) {
            Gson gson = new Gson();
            MODELS = gson.fromJson(
                    pref.getString("PERSONALIZATION_" + storePreference.getStringValue(GlobalData.SELECTED_MODEL), null),
                    new TypeToken<List<PersonalizationDataModel>>() {
                    }.getType());

            layoutLength = Objects.requireNonNull(MODELS).get(0).getColor().size();

            setImageGlide(MODELS.get(0).getColor().get(0).getImageUrl(), imagCarNew2);
            if (MODELS.size() > 1)
                //setWheelView(MODELS);
                setColorButtons(MODELS, variantClickPosition, colorClickPosition);
        } else {
            lloutWhole.setVisibility(View.GONE);
            imgNoData.setVisibility(View.VISIBLE);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setColorButtons(final List<PersonalizationDataModel> body, final int variantClickPosition, final int clickPosition) {
        linearLayoutColor.removeAllViews();
        if (layoutLength > 1)
            for (int i = 0; i < layoutLength; i++) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.personalization_button_layout, null);
                final LinearLayout linearLayout1 = view.findViewById(R.id.lineardrag);
                final TextView imageView = view.findViewById(R.id.txt_round);
                final TextView textView = view.findViewById(R.id.txt_name);
                final TextView txtWhite = view.findViewById(R.id.txt_white);
                view.setId(i);
                imageView.setId(i);
                textView.setId(i);
                textView.setVisibility(View.GONE);
                if (colorClickPosition == i)
                    txtWhite.setVisibility(View.VISIBLE);
                else
                    txtWhite.setVisibility(View.GONE);
                ColorStateList csl = new ColorStateList(new int[][]{{}}, new int[]{Color.parseColor(body.get(variantClickPosition).getColor().get(i).getColor().trim())});
                headerTextView.setText(storePreference.getStringValue(GlobalData.SELECTED_MODEL_NAME) + " - " + body.get(variantClickPosition).getColor().get(clickPosition).getColorTitle());
                headerTextView.setSelected(true);
                imageView.setBackgroundTintList(csl);
                linearLayout1.setId(i);
                linearLayout1.setTag(i + "");
                textView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        colorClickPosition = linearLayout1.getId();
                        setColorButtons(body, variantClickPosition, colorClickPosition);
                        headerTextView.setText(storePreference.getStringValue(GlobalData.SELECTED_MODEL_NAME) + " - " + body.get(variantClickPosition).getColor().get(colorClickPosition).getColorTitle().trim());
                        headerTextView.setSelected(true);
                        setImageGlide(body.get(variantClickPosition).getColor().get(colorClickPosition).getImageUrl(), imagCarNew2);
                    }
                });


                linearLayoutColor.addView(view);
            }
    }

    private void setWheelView(final List<GetAllAccessoriesByCategoryModel> body) {
        linear_wheel.removeAllViews();
        for (int i = 0; i < variantLength; i++) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.personalization_varient_layout, null);
            final LinearLayout linearLayout1 = view.findViewById(R.id.lineardrag);
            final ImageView bike_images = view.findViewById(R.id.img_access);
            view.setId(i);
            bike_images.setId(i);
            linearLayout1.setId(i);
            linearLayout1.setTag(i + "");
            setImageGlide(body.get(i).getImageUrl(), bike_images);
            linearImage.setBackgroundResource(R.drawable.border_grey);
            imagCarNew.setScaleType(ImageView.ScaleType.CENTER);
            setImageGlide(body.get(chooseVariantId).getImageUrl(), imagCarNew);
            if (chooseVariantId == i)
                bike_images.setBackgroundResource(R.drawable.border_grey);
            else
                bike_images.setBackgroundResource(R.drawable.custom_border);
            linearLayout1.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onClick(View v) {
                    imagCarNew = rootView.findViewById(R.id.imageCar1);
                    Log.e(linearLayout1.getId() + " position : ", String.valueOf(linearLayout1.getId()));
                    chooseVariantId = linearLayout1.getId();
                    txtTitle.setText(body.get(linearLayout1.getId()).getTitle());
                    txtDEsc.setText(body.get(linearLayout1.getId()).getDescription());
                    txtCode.setText(body.get(linearLayout1.getId()).getTitle());
                    if (body.get(linearLayout1.getId()).getPrice() != null && body.get(linearLayout1.getId()).getPrice().equalsIgnoreCase("0")) {
                        txtPrice.setVisibility(View.GONE);
                    } else {
//                        txtPrice.setVisibility(View.VISIBLE);
//                        txtPrice.setText("INR " + body.get(linearLayout1.getId()).getPrice() + " /-");
                    }

                    setWheelView(body);
                    //Glide.with(Objects.requireNonNull(getActivity())).load(body.get(linearLayout1.getId()).getImageUrl()).into(imagCarNew);

                }
            });
            linear_wheel.addView(view);
        }
    }

    private void setAccessoriesWheelView(final List<GetAllAccessoriesByCategoryModel> body) {
        linear_wheel.removeAllViews();
        for (int i = 0; i < ridersAccessoriesLength; i++) {
            if (body.get(i).getCatId().equalsIgnoreCase(accessId)) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.personalization_varient_layout, null);
                final LinearLayout linearLayout1 = view.findViewById(R.id.lineardrag);
                final ImageView bike_images = view.findViewById(R.id.img_access);
                view.setId(i);
                bike_images.setId(i);
                linearLayout1.setId(i);
                linearLayout1.setTag(i + "");
                setImageGlide(body.get(i).getImageUrl(), bike_images);
                setImageGlide(body.get(chooseId).getImageUrl(), imagCarNew);
                txtTitle.setText(body.get(chooseId).getTitle());

                txtDEsc.setText(body.get(chooseId).getDescription());

                txtCode.setText(body.get(chooseId).getTitle());
                body.get(linearLayout1.getId()).getPrice();
                if (body.get(chooseId).getPrice() != null && body.get(chooseId).getPrice().equalsIgnoreCase("0")) {
                    txtPrice.setVisibility(View.GONE);
                }

                linearImage.setBackgroundResource(R.drawable.border_grey);
                imagCarNew.setScaleType(ImageView.ScaleType.CENTER);
                if (chooseId == i)
                    bike_images.setBackgroundResource(R.drawable.border_grey);
                else
                    bike_images.setBackgroundResource(R.drawable.custom_border);

                linearLayout1.setOnClickListener(new View.OnClickListener() {
                    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void onClick(View v) {
                        imagCarNew = rootView.findViewById(R.id.imageCar1);
                        Log.e(linearLayout1.getId() + " position : ", String.valueOf(linearLayout1.getId()));
                        chooseId = linearLayout1.getId();
                        setImageGlide(body.get(chooseId).getImageUrl(), imagCarNew);
                        txtTitle.setText(body.get(chooseId).getTitle());
                        txtDEsc.setText(body.get(chooseId).getDescription());
                        txtCode.setText(body.get(chooseId).getTitle());
                        if (body.get(chooseId).getPrice() != null && body.get(chooseId).getPrice().equalsIgnoreCase("0")) {
                            txtPrice.setVisibility(View.GONE);
                        }
//                        } else {
//                            txtPrice.setVisibility(View.VISIBLE);
//                            txtPrice.setText(String.format("INR %s /-", body.get(chooseId).getPrice()));
//                        }
                        //Glide.with(Objects.requireNonNull(getActivity())).load(body.get(linearLayout1.getId()).getImageUrl()).into(imagCarNew);
                        setAccessoriesWheelView(body);
                    }
                });
                linear_wheel.addView(view);
            }
        }
    }

    private void setImageGlide(String imageUrl, final ImageView glideImageView) {
        Glide.with(Objects.requireNonNull(getActivity())).load(imageUrl)
                .thumbnail(Glide.with(getActivity()).load(R.drawable.gif_loader))
                .apply(new RequestOptions().placeholder(R.drawable.nodata)).listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                glideImageView.setImageDrawable(ContextCompat.getDrawable(Objects.requireNonNull(getActivity()), R.drawable.nodata));
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                if (isBiker) {
                    glideImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                }
                glideImageView.setImageDrawable(resource);
                return false;
            }
        }).into(glideImageView);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.logo:
                Objects.requireNonNull(getActivity()).onBackPressed();
                break;

            case R.id.btn_bikers:
                isBiker = true;
                imagCarNew = rootView.findViewById(R.id.imageCar1);
                imagCarNew.setImageResource(android.R.color.transparent);
                lloutDesc.setVisibility(View.VISIBLE);
                btn_bikers.setBackgroundResource(R.drawable.border_bottom_material_gray);
                btn_Riders.setBackgroundResource(android.R.color.transparent);
                spinnerRiderId.setVisibility(View.GONE);
                lloutSpn.setVisibility(View.GONE);
                linear_wheel.setVisibility(View.VISIBLE);
                bottomVariant.setVisibility(View.VISIBLE);
                accessoriesButtons.setVisibility(View.VISIBLE);
                linearLayoutColor.setVisibility(View.GONE);
                if (CommonMethods.isNetworkAvailable(Objects.requireNonNull(getActivity()))) {
                    getAllProductBasedOnId();
                }
                break;

            case R.id.btn_riders:
                isBiker = true;
                imagCarNew = rootView.findViewById(R.id.imageCar1);
                imagCarNew.setImageResource(android.R.color.transparent);
                lloutDesc.setVisibility(View.VISIBLE);
                btn_Riders.setBackgroundResource(R.drawable.border_bottom_material_gray);
                btn_bikers.setBackgroundResource(android.R.color.transparent);
                spinnerRiderId.setVisibility(View.VISIBLE);
                lloutSpn.setVisibility(View.VISIBLE);
                linear_wheel.setVisibility(View.VISIBLE);
                bottomVariant.setVisibility(View.VISIBLE);
                getAllAccessoriesDetails();
                break;

            case R.id.btn_accessories:
                isBiker = true;
                headerTextView.setVisibility(View.VISIBLE);
                headerTextView.setText(storePreference.getStringValue(GlobalData.SELECTED_MODEL_NAME));
                headerTextView.setSelected(true);
                imagCarNew = rootView.findViewById(R.id.imageCar1);
                imagCarNew2.setVisibility(View.GONE);
                imagCarNew.setImageResource(android.R.color.transparent);
                lloutDesc.setVisibility(View.VISIBLE);
                btn_bikers.setBackgroundResource(R.drawable.border_bottom_material_gray);
                btn_Riders.setBackgroundResource(android.R.color.transparent);
                spinnerRiderId.setVisibility(View.GONE);
                lloutSpn.setVisibility(View.GONE);
                linear_wheel.setVisibility(View.VISIBLE);
                bottomVariant.setVisibility(View.VISIBLE);
                lloutVariantSpn.setVisibility(View.GONE);
                accessoriesButtons.setVisibility(View.VISIBLE);
                linearLayoutColor.setVisibility(View.GONE);
                if (CommonMethods.isNetworkAvailable(Objects.requireNonNull(getActivity()))) {
                    getAllProductBasedOnId();
                }

                btnAccessories.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.material_bluegrey800));
                btnColor.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.grey));
                break;

            case R.id.btn_color:
                isBiker = false;
                imagCarNew2.setVisibility(View.VISIBLE);
                headerTextView.setVisibility(View.VISIBLE);
                imagCarNew = rootView.findViewById(R.id.imageCar1);
                linearImage.setBackgroundResource(android.R.color.transparent);
                imagCarNew.setImageResource(android.R.color.transparent);
                lloutDesc.setVisibility(View.GONE);
                variantClickPosition = 0;
                linearLayoutColor.setVisibility(View.VISIBLE);
                accessoriesButtons.setVisibility(View.GONE);
                linear_wheel.setVisibility(View.GONE);
                bottomVariant.setVisibility(View.GONE);
                lloutVariantSpn.setVisibility(View.VISIBLE);
                if (CommonMethods.isNetworkAvailable(Objects.requireNonNull(getActivity()))) {
                    getAllPersonalizationDetails();
                } else {
                    checkLocalPersonalization();
                }
                colorClickPosition = 0;
                setColorButtons(MODELS, variantClickPosition, colorClickPosition);
                btnColor.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.material_bluegrey800));
                btnAccessories.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(getActivity()), R.color.grey));
                break;

            default:
        }
    }


    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        imagCarNew = rootView.findViewById(R.id.imageCar1);
        imagCarNew.setImageResource(android.R.color.transparent);
        String selection =
                adapterView.getItemAtPosition(i).toString();
        if (BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList() != null && BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().size() > 0) {
            chooseId = 0;
            List<GetAllAccessoriesByCategoryModel> body = new ArrayList<>();
            Log.e("Selected Value", BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().get(i).getId() + selection);
            accessId = BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllCategoryModelList().get(i).getId();

            for (int k = 0; k < BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().size(); k++) {
                if (BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(k).getCatId().equalsIgnoreCase(accessId)) {
                    GetAllAccessoriesByCategoryModel byCategoryModel = new GetAllAccessoriesByCategoryModel();
                    byCategoryModel.setCatId(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(k).getCatId());

                    byCategoryModel.setDescription(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(k).getCatId());

                    byCategoryModel.setImageUrl(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(k).getImageUrl());

                    byCategoryModel.setPrice(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(k).getPrice());

                    byCategoryModel.setTitle(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(k).getTitle());

                    byCategoryModel.setModelId(BIKERS_MODEL.getGetAllAccessoriesModel().getGetAllAccessoriesByCategoryModels().get(k).getModelId());

                    body.add(byCategoryModel);

                }
            }
            ridersAccessoriesLength = body.size();
            setAccessoriesWheelView(body);
        }
    }

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

    }
}

