package com.cpm.lorealbaautomation.fragements;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.cpm.lorealbaautomation.Database.Lorealba_Database;
import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.breakmanagement.BreakManagementActivity;
import com.cpm.lorealbaautomation.competitionoffer.CompetitionOfferActivity;
import com.cpm.lorealbaautomation.constant.AlertandMessages;
import com.cpm.lorealbaautomation.constant.CommonString;
import com.cpm.lorealbaautomation.consumeractivity.ConsumerIntractionDetailActivity;
import com.cpm.lorealbaautomation.dailyactivity.BASurveyListActivity;
import com.cpm.lorealbaautomation.dailyactivity.BaAssessmentActivity;
import com.cpm.lorealbaautomation.dailyactivity.ConsumerReturnActivity;
import com.cpm.lorealbaautomation.dailyactivity.CounterImageActivity;
import com.cpm.lorealbaautomation.dailyactivity.CounterProfileActivity;
import com.cpm.lorealbaautomation.dailyactivity.DamagedCategoryActivity;
import com.cpm.lorealbaautomation.dailyactivity.GWPActivity;
import com.cpm.lorealbaautomation.dailyactivity.GroomedActivity;
import com.cpm.lorealbaautomation.dailyactivity.InWardStockActivity;
import com.cpm.lorealbaautomation.dailyactivity.MeQuizListActivity;
import com.cpm.lorealbaautomation.dailyactivity.RetailExcellenceActivity;
import com.cpm.lorealbaautomation.dailyactivity.SampleStockActivity;
import com.cpm.lorealbaautomation.dailyactivity.SkinGeniusCategoryActivity;
import com.cpm.lorealbaautomation.dailyactivity.StockCheckActivity;
import com.cpm.lorealbaautomation.dailyactivity.SuggestedStockActivity;
import com.cpm.lorealbaautomation.dailyactivity.TesterStockActivity;
import com.cpm.lorealbaautomation.dailyactivity.VisibilityElimentsActivity;
import com.cpm.lorealbaautomation.delegates.NavMenuItemGetterSetter;
import com.cpm.lorealbaautomation.delegates.PrefHelper;
import com.cpm.lorealbaautomation.geotag.GeoTaggingActivity;
import com.cpm.lorealbaautomation.gsonGetterSetter.JourneyPlan;
import com.cpm.lorealbaautomation.gsonGetterSetter.MasterPosm;
import com.cpm.lorealbaautomation.gsonGetterSetter.MasterSurvey;
import com.cpm.lorealbaautomation.gsonGetterSetter.ProductMaster;
import com.cpm.lorealbaautomation.gsonGetterSetter.TCounterInvoice;
import com.cpm.lorealbaautomation.leavemanagement.LeaveManagemntActivity;
import com.cpm.lorealbaautomation.makeupexpert.MakeoverActivity;
import com.cpm.lorealbaautomation.reports.MyLibraryActivity;

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

public class MyAdaperForIcon extends RecyclerView.Adapter<MyAdaperForIcon.ViewHolder> {
    List<NavMenuItemGetterSetter> data = Collections.emptyList();
    final LayoutInflater inflator;
    Context context;
    Lorealba_Database db;
    String counter_id, visit_date = "", userId, username, user_type, storeGeoTag = "N";
    SharedPreferences preferences;
    ArrayList<MasterPosm> promotionList = new ArrayList<>();
    String value;

    public MyAdaperForIcon(Context context, List<NavMenuItemGetterSetter> data, String value) {
        inflator = LayoutInflater.from(context);
        this.context = context;
        this.data = data;
        this.value = value;
        db = new Lorealba_Database(context);
        db.open();
        preferences = PrefHelper.getPrefs(context);
        counter_id = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        userId = preferences.getString(CommonString.KEY_USER_ID, "");
        username = preferences.getString(CommonString.KEY_USERNAME, "");
        user_type = preferences.getString(CommonString.KEY_USER_TYPE, "");
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        if (user_type.equalsIgnoreCase("ME")) {
            view = inflator.inflate(R.layout.custom_row_me, parent, false);
        } else {
            view = inflator.inflate(R.layout.custom_row, parent, false);
        }

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final NavMenuItemGetterSetter current = data.get(position);
        holder.list_icon.setImageResource(current.getIconImg());
        holder.list_icon.setId(position);
        holder.icon_txtname.setText(current.getIconName());
        holder.icon_txtname.setId(position);

        if (value.equals("1")) {
            if (current.getIconImg() == R.mipmap.counter_image || current.getIconImg() == R.mipmap.training || current.getIconImg() == R.mipmap.counter_img_tick) {
                holder.icon_txtname.setTextColor(context.getResources().getColor(R.color.black));
                holder.icon_txtname.setId(position);
            } else {
                holder.icon_txtname.setTextColor(context.getResources().getColor(R.color.background_color));
                holder.icon_txtname.setId(position);
            }
        } else if (value.equals("2")) {
            if (current.getIconImg() == R.mipmap.counter_image || current.getIconImg() == R.mipmap.b_mangement || current.getIconImg() == R.mipmap.training || current.getIconImg() == R.mipmap.counter_img_tick || current.getIconImg() == R.mipmap.geotag) {
                holder.icon_txtname.setTextColor(context.getResources().getColor(R.color.black));
                holder.icon_txtname.setId(position);
            } else {
                holder.icon_txtname.setTextColor(context.getResources().getColor(R.color.background_color));
                holder.icon_txtname.setId(position);
            }
        } else {
            holder.icon_txtname.setTextColor(context.getResources().getColor(R.color.black));
            holder.icon_txtname.setId(position);
        }

        String saleable_status = db.getStatus(CommonString.KEY_SaleableStatus, counter_id);
        String gwp_status = db.getStatus(CommonString.KEY_GwpStatus, counter_id);

        if (saleable_status != null && saleable_status.equalsIgnoreCase("Complete") && gwp_status != null && gwp_status.equalsIgnoreCase("Complete")) {
            if (current.getIconImg() == R.mipmap.damage_stock || current.getIconImg() == R.mipmap.inward_stock || current.getIconImg() == R.mipmap.consumer_interaction || current.getIconImg() == R.mipmap.consumer_return || current.getIconImg() == R.mipmap.competition_offers || current.getIconImg() == R.mipmap.retail_excellence || current.getIconImg() == R.mipmap.me_sale) {
                holder.icon_txtname.setTextColor(context.getResources().getColor(R.color.black));
                holder.icon_txtname.setId(position);
            }
        } else {
            if (current.getIconImg() == R.mipmap.damage_stock_grey || current.getIconImg() == R.mipmap.inward_stock_grey || current.getIconImg() == R.mipmap.consumer_intraction_grey || current.getIconImg() == R.mipmap.consumer_return_grey || current.getIconImg() == R.mipmap.competition_offers_gray || current.getIconImg() == R.mipmap.retail_excellence_grey || current.getIconImg() == R.mipmap.me_sales_grey) {
                holder.icon_txtname.setTextColor(context.getResources().getColor(R.color.background_color));
                holder.icon_txtname.setId(position);
            }
        }

        holder.dashbrd_card_view.setOnClickListener(v -> {
            if (current.getIconImg() == R.mipmap.counter_image || current.getIconImg() == R.mipmap.counter_img_tick) {
                context.startActivity(new Intent(context, CounterImageActivity.class));
            } else if (current.getIconImg() == R.mipmap.grooming || current.getIconImg() == R.mipmap.groomed_amber || current.getIconImg() == R.mipmap.groomed_green) {
                context.startActivity(new Intent(context, GroomedActivity.class));
            } else if (current.getIconImg() == R.mipmap.consumer_return) {
                context.startActivity(new Intent(context, ConsumerReturnActivity.class));
            } else if (current.getIconImg() == R.mipmap.stock_check) {
                context.startActivity(new Intent(context, StockCheckActivity.class));
            } else if (current.getIconImg() == R.mipmap.damage_stock) {
                context.startActivity(new Intent(context, DamagedCategoryActivity.class));
            } else if (current.getIconImg() == R.mipmap.sample_stock) {
                context.startActivity(new Intent(context, SampleStockActivity.class));
            } else if (current.getIconImg() == R.mipmap.suggested_stock) {
                context.startActivity(new Intent(context, SuggestedStockActivity.class));
            } else if (current.getIconImg() == R.mipmap.tester_stock) {
                context.startActivity(new Intent(context, TesterStockActivity.class));
            } else if (current.getIconImg() == R.mipmap.visibility) {
                promotionList = db.getVisibility_master_data(counter_id);
                if (!promotionList.isEmpty()) {
                    context.startActivity(new Intent(context, VisibilityElimentsActivity.class));
                } else {
                    AlertandMessages.showToastMsg(context, "Visibility Data Not Found");
                }
            } else if (current.getIconImg() == R.mipmap.inward_stock) {
                db.open();
                ArrayList<ProductMaster> stockList = db.getinwardStockTransactionList(counter_id, visit_date);
                ArrayList<TCounterInvoice> manualInwardStockList = db.getManualInwardStockList(counter_id, visit_date);
                if (!stockList.isEmpty()) {
                    db.deleteInwardStockData(counter_id, visit_date);
                }
                if (!manualInwardStockList.isEmpty()) {
                    db.deleteManualStockData(counter_id, visit_date);
                }

                context.startActivity(new Intent(context, InWardStockActivity.class));
            } else if (current.getIconImg() == R.mipmap.ba_survey) {
                db.open();
                JourneyPlan jcp_Object = db.getBIDfromJCP(username, counter_id);
                ArrayList<MasterSurvey> listDataHeader = db.getBASurveySavedData1("BA", jcp_Object, visit_date);
                if (!listDataHeader.isEmpty()) {
                    db.deleteBASurveyData(counter_id, visit_date);
                }
                context.startActivity(new Intent(context, BASurveyListActivity.class).putExtra("SurveyType", "BA"));
            } else if (current.getIconImg() == R.mipmap.ba_assessment) {
                context.startActivity(new Intent(context, BaAssessmentActivity.class));
            } else if (current.getIconImg() == R.mipmap.survey) {
                db.open();
                JourneyPlan jcp_Object = db.getBIDfromJCP(username, counter_id);
                ArrayList<MasterSurvey> listDataHeader = db.getBASurveySavedData1("Counter", jcp_Object, visit_date);
                if (!listDataHeader.isEmpty()) {
                    db.deleteCounterSurveyData(counter_id, visit_date);
                }
                context.startActivity(new Intent(context, BASurveyListActivity.class).putExtra("SurveyType", "Counter"));
            } else if (current.getIconImg() == R.mipmap.consumer_interaction) {
                context.startActivity(new Intent(context,
                        ConsumerIntractionDetailActivity.class).putExtra("from", CommonString.TAG_FROM_BA));
            } else if (current.getIconImg() == R.mipmap.leave_management) {
                context.startActivity(new Intent(context, LeaveManagemntActivity.class));
            } else if (current.getIconImg() == R.mipmap.b_mangement) {
                context.startActivity(new Intent(context, BreakManagementActivity.class));
            } else if (current.getIconImg() == R.mipmap.pwp_gwp) {
                context.startActivity(new Intent(context, GWPActivity.class));
            } else if (current.getIconImg() == R.mipmap.counterprofile) {
                context.startActivity(new Intent(context, CounterProfileActivity.class));
            } else if (current.getIconImg() == R.mipmap.training) {
                context.startActivity(new Intent(context, MyLibraryActivity.class).putExtra(CommonString.KEY_BODY, "1"));
            } else if (current.getIconImg() == R.mipmap.makeover) {
                context.startActivity(new Intent(context, MakeoverActivity.class));
            } else if (current.getIconImg() == R.mipmap.quiz) {
                context.startActivity(new Intent(context, MeQuizListActivity.class));
            } else if (current.getIconImg() == R.mipmap.me_sale) {
                // context.startActivity(new Intent(context, SalesActivity.class));
                context.startActivity(new Intent(context,
                        ConsumerIntractionDetailActivity.class).putExtra("from", CommonString.TAG_FROM_ME));
            }
            ///for Skin Genius
            else if (current.getIconImg() == R.mipmap.vto) {
                context.startActivity(new Intent(context, SkinGeniusCategoryActivity.class));
            } else if (current.getIconImg() == R.mipmap.competition_offers) {
                context.startActivity(new Intent(context, CompetitionOfferActivity.class));
            } else if (current.getIconImg() == R.mipmap.retail_excellence) {
                context.startActivity(new Intent(context, RetailExcellenceActivity.class));
            } else if (storeGeoTag.equals("N") && (current.getIconImg() == R.mipmap.geotag || current.getIconImg() == R.mipmap.geotag_done)) {
                context.startActivity(new Intent(context, GeoTaggingActivity.class));
            }
        });

        holder.dashbrd_card_view.setBackgroundResource(R.drawable.round_corner_parent);
        holder.dashbrd_card_view.setId(position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CardView dashbrd_card_view;
        ImageView list_icon;
        TextView icon_txtname;

        public ViewHolder(View view) {
            super(view);
            list_icon = (ImageView) view.findViewById(R.id.list_icon);
            icon_txtname = (TextView) view.findViewById(R.id.icon_txtname);
            dashbrd_card_view = (CardView) view.findViewById(R.id.dashbrd_card_view);
        }
    }
}
