package com.cpm.lorealbaMabeline.fragements;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cpm.lorealbaMabeline.Database.Lorealba_Database;
import com.cpm.lorealbaMabeline.DealarBoardActivity;
import com.cpm.lorealbaMabeline.R;
import com.cpm.lorealbaMabeline.constant.CommonString;
import com.cpm.lorealbaMabeline.delegates.NavMenuItemGetterSetter;
import com.cpm.lorealbaMabeline.delegates.PrefHelper;
import com.cpm.lorealbaMabeline.gsonGetterSetter.JourneyPlan;


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

/**
 * A fragment representing a list of Items.
 * <p/>
 * <p>
 * interface.
 */
public class CounterMenuFragment extends Fragment {
    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    JourneyPlan counterimg_object = new JourneyPlan();
    Lorealba_Database db;
    String counterId, visit_date = "", username;
    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public CounterMenuFragment() {

    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static CounterMenuFragment newInstance(int columnCount) {
        CounterMenuFragment fragment = new CounterMenuFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);


        return fragment;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            // TODO: Customize parameters
            int mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_item_list2, container, false);
        db = new Lorealba_Database(getActivity());
        db.open();
        SharedPreferences preferences = PrefHelper.getPrefs(getActivity());
        counterId = preferences.getString(CommonString.KEY_COUNTER_ID, "");
        visit_date = preferences.getString(CommonString.KEY_DATE, "");
        username = preferences.getString(CommonString.KEY_USERNAME, null);
        counterimg_object = db.getCounterProfileData(username, visit_date,counterId,false);
        // Set the adapter
        ((DealarBoardActivity) requireActivity()).setCounterMenuRefreshListener(value -> {
            // Set the adapter
            if (view instanceof RecyclerView) {
                Context context = view.getContext();
                RecyclerView recyclerView = (RecyclerView) view;
                recyclerView.setAdapter(new MyAdaperForIcon(context, getdata(value), value));
                recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            }
        });

        return view;
    }

    public List<NavMenuItemGetterSetter> getdata(String value) {
        List<NavMenuItemGetterSetter> data = new ArrayList<>();
        int counter_img = 0, stock_check = 0, tester_stock, pwp_gwp_stock = 0, sample_stock, damaged_stock, counter_survey = 0,
                visibility, inward_stock, counter_profile,competition_offers,retail_excellence,geoTag;
        db.open();
        String saleable_status = db.getStatus(CommonString.KEY_SaleableStatus,counterId);
        if (value.equals("1")) {
            db.open();
            if (db.isCounterChecked(visit_date, counterId)) {
                counter_img = R.mipmap.counter_img_tick;
            } else {
                counter_img = R.mipmap.counter_image;
            }
            stock_check = R.mipmap.stock_check_grey;
            tester_stock = R.mipmap.tester_stock_grey;
            pwp_gwp_stock = R.mipmap.pwp_gwp_grey;
            sample_stock = R.mipmap.sample_stock_grey;
            damaged_stock = R.mipmap.damage_stock_grey;
            visibility = R.mipmap.visibility_grey;
            competition_offers = R.mipmap.competition_offers_gray;
            retail_excellence = R.mipmap.retail_excellence_grey;
            inward_stock = R.mipmap.inward_stock_grey;
            counter_profile = R.mipmap.counterprofile_grey;
            counter_survey = R.mipmap.survey;
            geoTag = R.mipmap.geotag;
        } else if (value.equals("2")) {
            db.open();
            if (db.isCounterChecked(visit_date, counterId)) {
                counter_img = R.mipmap.counter_img_tick;
            } else {
                counter_img = R.mipmap.counter_image;
            }

            stock_check = R.mipmap.stock_check_grey;
            tester_stock = R.mipmap.tester_stock_grey;
            pwp_gwp_stock = R.mipmap.pwp_gwp_grey;
            sample_stock = R.mipmap.sample_stock_grey;
            damaged_stock = R.mipmap.damage_stock_grey;
            visibility = R.mipmap.visibility_grey;
            competition_offers = R.mipmap.competition_offers_gray;
            retail_excellence = R.mipmap.retail_excellence_grey;
            inward_stock = R.mipmap.inward_stock_grey;
            counter_profile = R.mipmap.counterprofile_grey;
            counter_survey = R.mipmap.survey;
            geoTag = R.mipmap.geotag;
        } else {
            db.open();
            if (db.isCounterChecked(visit_date, counterId)) {
                counter_img = R.mipmap.counter_img_tick;
            } else {
                counter_img = R.mipmap.counter_image;
            }
            if (saleable_status != null && saleable_status.equalsIgnoreCase("Complete")) {
                inward_stock = R.mipmap.inward_stock;
                damaged_stock = R.mipmap.damage_stock;
            } else {
                inward_stock = R.mipmap.inward_stock_grey;
                damaged_stock = R.mipmap.damage_stock_grey;
            }

            stock_check = R.mipmap.stock_check;
            tester_stock = R.mipmap.tester_stock;
            pwp_gwp_stock = R.mipmap.pwp_gwp;
            sample_stock = R.mipmap.sample_stock;
            visibility = R.mipmap.visibility;
            competition_offers = R.mipmap.competition_offers;
            retail_excellence = R.mipmap.retail_excellence;
            counter_profile = R.mipmap.counterprofile;
            counter_survey = R.mipmap.survey;

            geoTag =  R.mipmap.geotag;
        }


        int[] img = {counter_img, stock_check, tester_stock, pwp_gwp_stock, sample_stock, damaged_stock, counter_survey,visibility, inward_stock, counter_profile,competition_offers,retail_excellence,geoTag};
        String[] name = {"Counter Image", "Stock Check", "Tester", "GWP", "Sample", "Damaged", "Counter Survey","Visibility",
                "InWard Stock", "Counter Profile","Competition Intelligence","Retail Excellence","Counter GeoTag"};

        for (int i = 0; i < img.length; i++) {
            NavMenuItemGetterSetter recData = new NavMenuItemGetterSetter();
            recData.setIconImg(img[i]);
            recData.setIconName(name[i]);
            data.add(recData);
        }
        return data;
    }

}
