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 java.util.ArrayList;
import java.util.List;

/**
 * A fragment representing a list of Items.
 * <p/>
 * interface.
 */
public class BAdvisorMenuFragment extends Fragment {
    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    Lorealba_Database db;
    String counterId, visit_date = "", username, user_type;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public BAdvisorMenuFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static BAdvisorMenuFragment newInstance(int columnCount) {
        BAdvisorMenuFragment fragment = new BAdvisorMenuFragment();
        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_list4, 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);
        user_type = preferences.getString(CommonString.KEY_USER_TYPE, "");
        ((DealarBoardActivity) requireActivity()).setBaMenuRefreshListener(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 groomed_img = 0, consumer_interaction = 0, consumer_return = 0, break_mangment = 0, ba_training = 0, leave_mangment = 0, ba_survey = 0, ba_skin_genius = 0;
        db.open();
        String saleable_status = db.getStatus(CommonString.KEY_SaleableStatus, counterId);
        if (value.equals("1")) {
            groomed_img = R.mipmap.groomed_grey;
            consumer_interaction = R.mipmap.consumer_intraction_grey;
            consumer_return = R.mipmap.consumer_return_grey;
            break_mangment = R.mipmap.break_m_grey;
            ba_training = R.mipmap.training;
            if (user_type != null && user_type.equalsIgnoreCase(CommonString.TAG_FROM_BA)) {
                leave_mangment = R.mipmap.leave_m_grey;
            }
            ba_survey = R.mipmap.ba_survey;
            ba_skin_genius = R.mipmap.vto_grey;

        } else if (value.equals("2")) {
            groomed_img = R.mipmap.groomed_grey;
            consumer_interaction = R.mipmap.consumer_intraction_grey;
            consumer_return = R.mipmap.consumer_return_grey;
            break_mangment = R.mipmap.b_mangement;
            if (user_type != null && user_type.equalsIgnoreCase(CommonString.TAG_FROM_BA)) {
                leave_mangment = R.mipmap.leave_m_grey;
            }
            ba_training = R.mipmap.training;
            ba_survey = R.mipmap.ba_survey;
            ba_skin_genius = R.mipmap.vto_grey;
        } else {
            db.open();
            boolean complete = saleable_status != null && saleable_status.equalsIgnoreCase("Complete");
            if (db.isGroomedChecked(counterId, username, visit_date).equalsIgnoreCase("Green")) {
                if (complete) {
                    consumer_return = R.mipmap.consumer_return;
                    consumer_interaction = R.mipmap.consumer_interaction;
                    ba_skin_genius = R.mipmap.vto;
                } else {
                    consumer_return = R.mipmap.consumer_return_grey;
                    consumer_interaction = R.mipmap.consumer_intraction_grey;
                    ba_skin_genius = R.mipmap.vto_grey;
                }
                groomed_img = R.mipmap.groomed_green;
                break_mangment = R.mipmap.b_mangement;
                ba_training = R.mipmap.training;
                if (user_type != null && user_type.equalsIgnoreCase(CommonString.TAG_FROM_BA)) {
                    leave_mangment = R.mipmap.leave_management;
                }
                ba_survey = R.mipmap.ba_survey;


            } else if (db.isGroomedChecked(counterId, username, visit_date).equalsIgnoreCase("Amber")) {
                if (complete) {
                    consumer_return = R.mipmap.consumer_return;
                    consumer_interaction = R.mipmap.consumer_interaction;
                    ba_skin_genius = R.mipmap.vto;
                } else {
                    consumer_return = R.mipmap.consumer_return_grey;
                    consumer_interaction = R.mipmap.consumer_intraction_grey;
                    ba_skin_genius = R.mipmap.vto_grey;
                }
                groomed_img = R.mipmap.groomed_amber;
                break_mangment = R.mipmap.b_mangement;
                ba_training = R.mipmap.training;
                if (user_type != null && user_type.equalsIgnoreCase(CommonString.TAG_FROM_BA)) {
                    leave_mangment = R.mipmap.leave_management;
                }
                ba_survey = R.mipmap.ba_survey;

            } else {
                if (complete) {
                    consumer_return = R.mipmap.consumer_return;
                    consumer_interaction = R.mipmap.consumer_interaction;
                    ba_skin_genius = R.mipmap.vto;
                } else {
                    consumer_return = R.mipmap.consumer_return_grey;
                    consumer_interaction = R.mipmap.consumer_intraction_grey;
                    ba_skin_genius = R.mipmap.vto_grey;
                }

                groomed_img = R.mipmap.grooming;
                break_mangment = R.mipmap.b_mangement;
                ba_training = R.mipmap.training;
                if (user_type != null && user_type.equalsIgnoreCase(CommonString.TAG_FROM_BA)) {
                    leave_mangment = R.mipmap.leave_management;
                }
                ba_survey = R.mipmap.ba_survey;
            }
        }

        int[] img = {groomed_img, consumer_interaction, consumer_return, break_mangment, ba_training, ba_survey, ba_skin_genius};
        String[] name = {"Grooming Image", "Consumer Interaction", "Consumer Return", "Break Management", "Training", "BA Survey", "VTO"};

        if (user_type != null && user_type.equalsIgnoreCase(CommonString.TAG_FROM_BA)) {
            name = new String[]{"Grooming Image", "Consumer Interaction", "Consumer Return", "Break Management", "Leave Management", "Training", "BA Survey", "VTO"};
            img = new int[]{groomed_img, consumer_interaction, consumer_return, break_mangment, leave_mangment, ba_training, ba_survey, ba_skin_genius};
        }
        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;
    }

}
