package cpm.com.gskmtorange.gsk_dailyentry;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import cpm.com.gskmtorange.Database.GSKOrangeDB;
import cpm.com.gskmtorange.GetterSetter.AddittionalGetterSetter;
import cpm.com.gskmtorange.GetterSetter.CategoryPictureGetterSetter;
import cpm.com.gskmtorange.R;
import cpm.com.gskmtorange.adapter.data.ImageUri;
import cpm.com.gskmtorange.constant.CommonFunctions;
import cpm.com.gskmtorange.constant.CommonString;
import cpm.com.gskmtorange.constant.CommonUtils;
import cpm.com.gskmtorange.dailyentry.AdditionalPromoSKUwiseActivity;
import cpm.com.gskmtorange.dailyentry.AdditionalVisibility;
import cpm.com.gskmtorange.dailyentry.AuditActivity;
import cpm.com.gskmtorange.dailyentry.CategoryPicture;
import cpm.com.gskmtorange.dailyentry.CompetitionPromoActivity;
import cpm.com.gskmtorange.dailyentry.CounterfeitProductsActivity;
import cpm.com.gskmtorange.dailyentry.CreateSelfActivity;
import cpm.com.gskmtorange.dailyentry.NewStockDataActivity;
import cpm.com.gskmtorange.dailyentry.OrderTakingActivity;
import cpm.com.gskmtorange.dailyentry.PDVisibilityActivity;
import cpm.com.gskmtorange.dailyentry.PDimageActivity;
import cpm.com.gskmtorange.dailyentry.POGQuestionsActivity;
import cpm.com.gskmtorange.dailyentry.StockActivity;
import cpm.com.gskmtorange.dailyentry.StockDataActivity;
import cpm.com.gskmtorange.dailyentry.StockPricingActivity;
import cpm.com.gskmtorange.dailyentry.T2PComplianceActivity;
import cpm.com.gskmtorange.xmlGetterSetter.AuditDataGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.DailyDataMenuGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.T2PGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.Visit_Type_Master_GetterSetter;

public class DailyDataMenuActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    ArrayList<DailyDataMenuGetterSetter> categoryList;
    DailyDataMenuAdapter adapter;
    TextView txt_categoryName;
    private Context context;
    GSKOrangeDB db;
    String categoryName = "", categoryId;
    String store_id, visit_date, username, intime, date, keyAccount_id, class_id, storeType_id, camera_allow, country_id, pog_type_id, store_flag_str;
    private SharedPreferences preferences;
    ArrayList<CategoryPictureGetterSetter> category_camera_list;
    ArrayList<Visit_Type_Master_GetterSetter> visittypelist = new ArrayList<Visit_Type_Master_GetterSetter>();
    String visit_type_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.activity_daily_data_menu);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            //txt_categoryName = (TextView) findViewById(R.id.txt_categoryName);
            db = new GSKOrangeDB(this);
            db.open();
            context = this;
            //preference data
            //deletefile(context);
            preferences = PreferenceManager.getDefaultSharedPreferences(this);
            CommonFunctions.updateLangResources(context, preferences.getString(CommonString.KEY_LANGUAGE, ""));
            store_id = preferences.getString(CommonString.KEY_STORE_ID, null);
            visit_date = preferences.getString(CommonString.KEY_DATE, null);
            date = preferences.getString(CommonString.KEY_DATE, null);
            username = preferences.getString(CommonString.KEY_USERNAME, null);
            intime = preferences.getString(CommonString.KEY_STORE_IN_TIME, "");
            keyAccount_id = preferences.getString(CommonString.KEY_KEYACCOUNT_ID, "");
            class_id = preferences.getString(CommonString.KEY_CLASS_ID, "");
            storeType_id = preferences.getString(CommonString.KEY_STORETYPE_ID, "");
            camera_allow = preferences.getString(CommonString.KEY_CAMERA_ALLOW, "");
            country_id = preferences.getString(CommonString.KEY_COUNTRY_ID, "");
            pog_type_id = preferences.getString(CommonString.KEY_POG_TYPE_ID, "");
            store_flag_str = preferences.getString(CommonString.KEY_STORE_FLAG, null);
            if (preferences.getString(CommonString.KEY_VISIT_ID, "") != null) {
                visit_type_id = preferences.getString(CommonString.KEY_VISIT_ID, "");
            }
            //Intent data
            categoryName = getIntent().getStringExtra("categoryName");
            categoryId = getIntent().getStringExtra("categoryId");
            toolbar.setTitle(getResources().getString(R.string.title_activity_daily_main_menu) + " - " + categoryName);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            db.open();
           // deleteIRURl(context);
            visittypelist = db.getInsertedVisitType(store_id, visit_date, "3");
            if (visittypelist.size() > 0) {
                if (visittypelist.get(0).getVISIT_TYPE_ID().equals("3")) {

                }
            }
        } catch (Resources.NotFoundException e) {
            e.fillInStackTrace();
        }
    }

    private void deleteIRURl(Context context) {
        try {
            ArrayList<CategoryPictureGetterSetter> irList = db.getIrPDforUpload(store_id, visit_date);
            Log.d("irlistsize", String.valueOf(irList));
            if (irList != null && !irList.isEmpty()) {
                for (int i = 0; i < irList.size(); i++) {
                    ArrayList<ImageUri> uris = irList.get(i).getImageUris();
                    if (uris != null && !uris.isEmpty()) {
                        for (int k = 0; k < uris.size(); k++) {
                          //  CommonUtils.INSTANCE.deleteSpecificImage(this,uris.get(k).getUri());
                            deleteSpecificImage(context,uris.get(k).getUri());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.fillInStackTrace();
        }
    }

    private void deleteSpecificImage(Context context, String uri) {
        // Path to the file in app-specific internal storage
        String filePath = uri;
        Log.d("filepath",filePath);

        // Create a File object
        File file = new File(filePath);
        // Check if the file exists and delete it
        if (file.exists()) {
            boolean isDeleted = file.delete();
            if (isDeleted) {
                Toast.makeText(context, "File deleted successfully", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Failed to delete the file", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "File does not exist", Toast.LENGTH_SHORT).show();
        }
    }

    private void deletefile(Context context) {
        // Path to the file in app-specific internal storage
        String filePath = context.getFilesDir().getAbsolutePath() + "/e462845c-0dc4-4aa0-9066-6334d33af7d8_1.jpg";
        Log.d("filepathnew",filePath);

        // Create a File object
        File file = new File(filePath);
        // Check if the file exists and delete it
        if (file.exists()) {
            boolean isDeleted = file.delete();
            if (isDeleted) {
                Toast.makeText(context, "File deleted successfully", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Failed to delete the file", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "File does not exist", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        CommonFunctions.updateLangResources(context, preferences.getString(CommonString.KEY_LANGUAGE, ""));
        try {
            categoryList = new ArrayList<>();
            DailyDataMenuGetterSetter data = new DailyDataMenuGetterSetter();
            //For Additional Stores in Egypt(6) Only Stock Data will be captured
            if (store_flag_str.equalsIgnoreCase(CommonString.FROM_ADDITIONAL)) {
                data = new DailyDataMenuGetterSetter();
                data.setCategory_name(getResources().getString(R.string.stock));
                if (isMappingStockData()) {
                    if (db.checkStockData(store_id, categoryId, visit_date)) {
                        data.setCategory_img(R.mipmap.msl_availability_done);
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability);
                    }
                } else {
                    data.setCategory_img(R.mipmap.msl_availability_grey);
                }
                categoryList.add(data);
            } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION) && country_id.equals("2") && visit_type_id.equals("3")) {
                data = new DailyDataMenuGetterSetter();
                data.setCategory_name(getResources().getString(R.string.daily_data_menu_additional_visibility));
                if (db.additionalVisibilitydata(store_id, categoryId)) {
                    data.setCategory_img(R.mipmap.additional_visibility_done);
                } else {
                    data.setCategory_img(R.mipmap.additional_visibility);
                }
                categoryList.add(data);
            } else {
                if (country_id.equals(CommonString.COUNTRY_ID_EGYPT + "") && categoryId.equals("3")) {
                    data = new DailyDataMenuGetterSetter();
                    data.setCategory_name(getResources().getString(R.string.daily_data_menu_stock_facing));
                    if (isMappingStockData()) {
                        if (db.checkMsl_Availability_StockFacingDataNew(store_id, categoryId, visit_date)) {
                            data.setCategory_img(R.mipmap.msl_availability_done);
                        } else {
                            data.setCategory_img(R.mipmap.msl_availability);
                        }
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability_grey);
                    }
                    categoryList.add(data);
                } else {
                    data = new DailyDataMenuGetterSetter();
                    data.setCategory_name(getResources().getString(R.string.daily_data_menu_msl_availability));
                    if (isMappingStockData()) {
                        if (db.checkMsl_Availability_StockFacingDataNew(store_id, categoryId, visit_date)) {
                            data.setCategory_img(R.mipmap.msl_availability_done);
                        } else {
                            data.setCategory_img(R.mipmap.msl_availability);
                        }
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability_grey);
                    }
                    categoryList.add(data);
                }


                //T2p  and kenya POSm
                data = new DailyDataMenuGetterSetter();
                if (country_id.equals("8") || country_id.equals("17")) {
                    data.setCategory_name(getResources().getString(R.string.title_posm));
                } else {
                    data.setCategory_name(getResources().getString(R.string.daily_data_menu_t2p));
                }

                boolean flag_t2p_mapping;
                if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                    flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P_ADHOC);
                } else {
                    flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P);
                }

                if (flag_t2p_mapping) {
                    if (db.isFilledT2P(store_id, categoryId)) {
                        data.setCategory_img(R.mipmap.t2p_compliance_done);
                    } else {
                        data.setCategory_img(R.mipmap.t2p_compliance);
                    }
                } else {
                    data.setCategory_img(R.mipmap.t2pcompliance_grey);
                }
                categoryList.add(data);


                //T2p added
                data = new DailyDataMenuGetterSetter();
                if (country_id.equals("17")) {
                    data.setCategory_name(getResources().getString(R.string.title_posm) + "/" + getResources().getString(R.string.daily_data_menu_additional_visibility));
                } else {
                    data.setCategory_name(getResources().getString(R.string.daily_data_menu_additional_visibility));
                }
                // data.setCategory_name(getResources().getString(R.string.daily_data_menu_additional_visibility));
                if (db.additionalVisibilitydata(store_id, categoryId)) {
                    data.setCategory_img(R.mipmap.additional_visibility_done);
                } else {
                    data.setCategory_img(R.mipmap.additional_visibility);
                }
                categoryList.add(data);

                data = new DailyDataMenuGetterSetter();
                data.setCategory_name(getResources().getString(R.string.daily_data_menu_promo_compliance));
                if (country_id.equals("7")) {
                    if (db.getPromoComplianceSkuwiseExistsAfterData(store_id, categoryId).size() == 0) {
                        data.setCategory_img(R.mipmap.promo_compliance);
                    } else {
                        data.setCategory_img(R.mipmap.promo_compliance_done);
                    }
                } else {
                    boolean isMappingPromotion_Flag;
                    if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        isMappingPromotion_Flag = db.isMappingPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_PROMOTION_ADHOC);
                    } else {
                        isMappingPromotion_Flag = db.isMappingPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_PROMOTION);
                    }

                    boolean isMappingAdditionalPromotion_Flag;
                    if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        isMappingAdditionalPromotion_Flag = db.isMappingAdditionalPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_ADDITIONAL_PROMOTION_ADHOC);
                    } else {
                        isMappingAdditionalPromotion_Flag = db.isMappingAdditionalPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_ADDITIONAL_PROMOTION);
                    }

                    if (isMappingPromotion_Flag || isMappingAdditionalPromotion_Flag) {
                        if (db.checkPromoComplianceData(store_id, categoryId) || db.checkAdditionalPromoComplianceData(store_id, categoryId)) {
                            data.setCategory_img(R.mipmap.promo_compliance_done);
                        } else {
                            data.setCategory_img(R.mipmap.promo_compliance);
                        }
                    } else {
                        data.setCategory_img(R.mipmap.promocompliance_grey);
                    }
                }
                categoryList.add(data);

                //Category Pictures
                data = new DailyDataMenuGetterSetter();
                data.setCategory_name(getResources().getString(R.string.daily_data_menu_category_picture));
                //category_camera_list = db.getCategoryPicturedata(categoryId, keyAccount_id, storeType_id, class_id);
                if (camera_allow.equalsIgnoreCase("1")) {
                    //if (category_camera_list.size()>0) {
                    if (db.isCategoryPictureData(store_id, categoryId)) {
                        data.setCategory_img(R.mipmap.picturecatogory_done);
                    } else {
                        data.setCategory_img(R.mipmap.picturecatogory);
                        //   }
                    } /*else {
                data.setCategory_img(R.mipmap.picturecatogory_grey);
            }*/
                } else if (country_id != null && country_id.equals("17") && camera_allow.equalsIgnoreCase("0")) {
                    data.setCategory_img(R.mipmap.picturecatogory_grey);
                } else {
                    if (db.getNoCameraCategoryDataInserted(store_id, categoryId, null).size() > 0) {
                        data.setCategory_img(R.mipmap.picturecatogory_done);
                    } else {
                        data.setCategory_img(R.mipmap.picturecatogory);
                        // }
                    }
                }
                categoryList.add(data);

                //Order Taking
                data = new DailyDataMenuGetterSetter();
                // ArrayList<AuditDataGetterSetter> question_list;
                data.setCategory_img(R.mipmap.msl_availability);
                if (country_id.equals("15") || (country_id.equals("17") && categoryId.equals("2"))) {
                    if (isMappingStockData()) {
                        if (db.checkOrder_taking_Data(store_id, categoryId)) {
                            data.setCategory_img(R.mipmap.msl_availability_done);
                        } else {
                            data.setCategory_img(R.mipmap.msl_availability);
                        }
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability_grey);
                    }
                    data.setCategory_name(getString(R.string.order_taking));
                    categoryList.add(data);
                }

                //Order Confirm
                data = new DailyDataMenuGetterSetter();
                // ArrayList<AuditDataGetterSetter> question_list;
                if (country_id.equals("15")) {
                    if (db.checkOrder_Received_Data(store_id, categoryId)) {
                        data.setCategory_img(R.mipmap.msl_availability_done);
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability);
                    }
                    data.setCategory_name(getString(R.string.order_confirm));
                    categoryList.add(data);
                }

                //Audit
                data = new DailyDataMenuGetterSetter();

                ArrayList<AuditDataGetterSetter> question_list;

                question_list = getAuditQnsRemoved();

                if (question_list.size() > 0) {
                    if (db.getAfterSaveAuditQuestionAnswerData(store_id, categoryId).size() > 0) {
                        data.setCategory_img(R.mipmap.msl_availability_done);
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability);
                    }
                } else {
                    data.setCategory_img(R.mipmap.msl_availability_grey);
                }

                data.setCategory_name(getString(R.string.audit));
                categoryList.add(data);

                //POG
              /*  data = new DailyDataMenuGetterSetter();
                if (db.getSubCategoryMasterForPOG(categoryId, storeType_id, pog_type_id, null).size() > 0) {
                    if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, categoryId).size() > 0) {
                        data.setCategory_img(R.mipmap.msl_availability_done);
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability);
                    }

                } else {
                    data.setCategory_img(R.mipmap.msl_availability_grey);
                }*/
                //new add UAE Pharma
                data = new DailyDataMenuGetterSetter();
                if (country_id.equals("2")) {
                    if (camera_allow.equals("1")) {
                        if (db.getSubCategoryMasterForPOG(categoryId, storeType_id, pog_type_id, null).size() > 0) {
                            if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, categoryId).size() > 0) {
                                data.setCategory_img(R.mipmap.msl_availability_done);
                            } else {
                                data.setCategory_img(R.mipmap.msl_availability);
                            }

                        } else {
                            data.setCategory_img(R.mipmap.msl_availability_grey);
                        }
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability_grey);
                    }
                } else {
                    if (db.getSubCategoryMasterForPOG(categoryId, storeType_id, pog_type_id, null).size() > 0) {
                        if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, categoryId).size() > 0) {
                            data.setCategory_img(R.mipmap.msl_availability_done);
                        } else {
                            data.setCategory_img(R.mipmap.msl_availability);
                        }
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability_grey);
                    }
                }

                data.setCategory_name(getString(R.string.pog));
                categoryList.add(data);

                //Warehouse Stock - Jordan
                if (country_id.equals("10")) {
                    data = new DailyDataMenuGetterSetter();
                    data.setCategory_name(getResources().getString(R.string.stock_entry));
                    if (isMappingStockData()) {
                        if (db.checkStock_Entry_FacingDataNew(store_id, categoryId, visit_date)) {
                            data.setCategory_img(R.mipmap.msl_availability_done);
                        } else {
                            data.setCategory_img(R.mipmap.msl_availability);
                        }
                    } else {
                        data.setCategory_img(R.mipmap.msl_availability_grey);
                    }
                    categoryList.add(data);
                }

                //Counterfeit Product Option - Egypt
                if (country_id.equals("6")) {
                    data = new DailyDataMenuGetterSetter();

                    if (db.getCounterfeitProductExistsAfterData(store_id, categoryId).size() > 0) {
                        data.setCategory_img(R.mipmap.counterfeit_done);
                    } else {
                        data.setCategory_img(R.mipmap.counterfeit);
                    }

                    data.setCategory_name(getString(R.string.title_activity_counterfeit_products));
                    categoryList.add(data);
                }

                //usk
                //Cometition promo Option - Egypt
                if (categoryId.equals("2")) {
                    if (country_id.equals("6")) {
                        data = new DailyDataMenuGetterSetter();
                        if (db.getCompetitionPromoExistsAfterData(store_id, categoryId).size() > 0) {
                            data.setCategory_img(R.mipmap.competition_done);
                        } else {
                            data.setCategory_img(R.mipmap.competition);
                        }

                        data.setCategory_name(getString(R.string.title_activity_competition_promo));
                        categoryList.add(data);

                        data = new DailyDataMenuGetterSetter();
                        data.setCategory_img(R.mipmap.competition);
                        data.setCategory_name("");
                        categoryList.add(data);
                    }
                }

                //IR Images
                data = new DailyDataMenuGetterSetter();
                ArrayList<CategoryPictureGetterSetter> ir_list = isPdIRMapping();
                if (ir_list != null && !ir_list.isEmpty() && camera_allow.equals("1")) {
                    data.setCategory_img(R.mipmap.picturecatogory);
                    db.open();
                    if (db.isSOSCategory(store_id, visit_date, CommonString.KEY_COMPELETE, categoryId)) {
                        data.setCategory_img(R.mipmap.picturecatogory_done);
                    }
                } else {
                    data.setCategory_img(R.mipmap.picturecatogory_grey);
                }
                data.setCategory_name(getResources()
                        .getString(R.string.title_activity_ir_image));
                categoryList.add(data);

                //IR Visibility
                data = new DailyDataMenuGetterSetter();
                ArrayList<CategoryPictureGetterSetter> ir_visibility = isPdVisibiltyMapping();
                if (ir_visibility != null && !ir_visibility.isEmpty() && camera_allow.equals("1")) {
                    data.setCategory_img(R.mipmap.picturecatogory);
                    db.open();
                    if (db.isPdVisibility(store_id, visit_date, CommonString.KEY_COMPELETE, categoryId)) {
                        data.setCategory_img(R.mipmap.picturecatogory_done);
                    }
                } else {
                    data.setCategory_img(R.mipmap.picturecatogory_grey);
                }

            }
            data.setCategory_name(getResources()
                        .getString(R.string.title_activity_ir_visibility));
                categoryList.add(data);

            adapter = new DailyDataMenuAdapter(DailyDataMenuActivity.this, categoryList);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
        } catch (Resources.NotFoundException e) {
            e.fillInStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == android.R.id.home) {
            finish();
        }

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    ArrayList<AuditDataGetterSetter> getAuditQnsRemoved() {
        boolean t2p_flag = false;
        boolean flag_t2p_mapping;
        if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
            flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P_ADHOC);
        } else {
            flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P);
        }
        if (flag_t2p_mapping) {
            if (db.isFilledT2P(store_id, categoryId)) {
                ArrayList<T2PGetterSetter> t2PList = db.getT2pComplianceData(store_id, categoryId);
                for (int i = 0; i < t2PList.size(); i++) {
                    if (t2PList.get(i).getPresent() == 1) {
                        t2p_flag = true;
                        break;
                    }
                }
            }
        }

        if (!t2p_flag) {
            if (db.additionalVisibilitydata(store_id, categoryId)) {
                ArrayList<AddittionalGetterSetter> additionalList = db.getAdditionalMainStock(store_id, categoryId);
                for (int i = 0; i < additionalList.size(); i++) {
                    if (additionalList.get(i).getBtn_toogle().equals("1")) {
                        t2p_flag = true;
                        break;
                    }
                }
            }
        }

        ArrayList<AuditDataGetterSetter> question_list = db.getAuditCategoryWise(categoryId, storeType_id);
        Log.e("catid", categoryId + "," + storeType_id + "," + question_list.size());
        Iterator<AuditDataGetterSetter> iterator = question_list.iterator();
        while (iterator.hasNext()) {
            AuditDataGetterSetter item = iterator.next(); // must be called before you can call iterator.remove()
            // Check condition
            if (!item.getKEYACCOUNT_ID().equals("0") && !item.getKEYACCOUNT_ID().equals(keyAccount_id)) {
                iterator.remove();
            } else if (!item.getCHECK_TYPE().equals("NA") && item.getCHECK_TYPE().equals("T2P")) {
                if (!t2p_flag) {
                    iterator.remove();
                }
            }
        }
        Log.e("questionlistsize", String.valueOf(question_list));
        return question_list;
    }

    private class DailyDataMenuAdapter extends RecyclerView.Adapter<DailyDataMenuAdapter.MyViewHolder> {
        List<DailyDataMenuGetterSetter> list = Collections.emptyList();
        Context context;
        LayoutInflater inflator;

        public DailyDataMenuAdapter(Context context, List<DailyDataMenuGetterSetter> list) {
            inflator = LayoutInflater.from(context);
            this.list = list;
            this.context = context;
        }

        @NonNull
        @Override
        public DailyDataMenuAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            View view = inflator.inflate(R.layout.daily_main_menu_row, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final DailyDataMenuAdapter.MyViewHolder holder, int position) {
            final DailyDataMenuGetterSetter dailyData = list.get(position);
            holder.categoryName.setText(dailyData.getCategory_name());
            holder.categoryIcon.setImageResource(dailyData.getCategory_img());
            holder.irview.setVisibility(View.GONE);
            db.open();
            visittypelist = db.getInsertedVisitType(store_id, visit_date, "3");
            if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.daily_data_menu_promo_compliance))) {
                if (country_id.equals("7")) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else {
                    boolean isMappingPromotion_Flag;
                    if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        isMappingPromotion_Flag = db.isMappingPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_PROMOTION_ADHOC);
                    } else {
                        isMappingPromotion_Flag = db.isMappingPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_PROMOTION);
                    }
                    boolean isMappingAdditionalPromotion_Flag;
                    if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        isMappingAdditionalPromotion_Flag = db.isMappingAdditionalPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_ADDITIONAL_PROMOTION_ADHOC);
                    } else {
                        isMappingAdditionalPromotion_Flag = db.isMappingAdditionalPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_ADDITIONAL_PROMOTION);
                    }
                    if (isMappingPromotion_Flag || isMappingAdditionalPromotion_Flag) {
                        holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                    } else {
                        holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                    }
                }
            } else if (dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.daily_data_menu_t2p))) || dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.title_posm)))) {
                boolean t2p_flag = false;
                boolean flag_t2p_mapping;
                if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                    flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P_ADHOC);
                } else {
                    flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P);
                }
                if (flag_t2p_mapping) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                }
            } else if (dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.daily_data_menu_category_picture)))) {
                if (camera_allow.equalsIgnoreCase("1")) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else if (country_id.equals("17") && !camera_allow.equalsIgnoreCase("1")) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                }
            } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.daily_data_menu_msl_availability))) {
                if (isMappingStockData()) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                }
            } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.stock_entry))) {
                if (isMappingStockData()) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                }
            } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.daily_data_menu_stock_facing)) || dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.stock))) {
                if (isMappingStockData()) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                }
            } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.audit))) {
                if (!getAuditQnsRemoved().isEmpty()) {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                } else {
                    holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                }
            } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.pog))) {
                if (country_id.equals("2")) {
                    if (camera_allow.equals("1")) {
                        if (!db.getSubCategoryMasterForPOG(categoryId, storeType_id, pog_type_id, null).isEmpty()) {
                            holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                        } else {
                            holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                        }
                    } else {
                        holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                    }
                } else {
                    db.open();
                    if (!db.getSubCategoryMasterForPOG(categoryId, storeType_id, pog_type_id, null).isEmpty()) {
                        holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.black));
                    } else {
                        holder.categoryName.setTextColor(ContextCompat.getColor(context, R.color.grey_background));
                    }
                }
            }
            else if(dailyData.getCategory_name().equalsIgnoreCase("")){
                holder.lay_menu.setVisibility(View.GONE);
                holder.irview.setVisibility(View.GONE);
                holder.categoryName.setVisibility(View.GONE);
            }
            else if (dailyData.getCategory_name().equalsIgnoreCase(getString(R.string.title_activity_ir_image))) {
                holder.irview.setVisibility(View.VISIBLE);
                holder.categoryName.setTextColor(isPdIRMapping() != null && !isPdIRMapping().isEmpty() && camera_allow.equals("1")
                        ? ContextCompat.getColor(context, R.color.black) : ContextCompat.getColor(context, R.color.grey_background));
            }
            else if (dailyData.getCategory_name().equalsIgnoreCase(getString(R.string.title_activity_ir_visibility))) {
                holder.irview.setVisibility(View.VISIBLE);
                holder.categoryName.setTextColor(isPdVisibiltyMapping() != null && !isPdVisibiltyMapping().isEmpty() && camera_allow.equals("1")
                        ? ContextCompat.getColor(context, R.color.black) : ContextCompat.getColor(context, R.color.grey_background));
            }
            else{
                holder.irview.setVisibility(View.GONE);
            }

            holder.lay_menu.setOnClickListener(view -> {
                if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.daily_data_menu_promo_compliance))) {
                    if (country_id.equals("7")) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, AdditionalPromoSKUwiseActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                    } else {
                        boolean isMappingPromotion_Flag;
                        if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                            isMappingPromotion_Flag = db.isMappingPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_PROMOTION_ADHOC);
                        } else {
                            isMappingPromotion_Flag = db.isMappingPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_PROMOTION);
                        }

                        boolean isMappingAdditionalPromotion_Flag;
                        if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                            isMappingAdditionalPromotion_Flag = db.isMappingAdditionalPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_ADDITIONAL_PROMOTION_ADHOC);
                        } else {
                            isMappingAdditionalPromotion_Flag = db.isMappingAdditionalPromotionData(store_id, categoryId, CommonString.TABLE_MAPPING_ADDITIONAL_PROMOTION);
                        }

                        if (isMappingPromotion_Flag || isMappingAdditionalPromotion_Flag) {
                            Intent intent = new Intent(DailyDataMenuActivity.this, PromoComplianceActivity.class);
                            intent.putExtra("categoryName", dailyData.getCategory_name());
                            intent.putExtra("categoryId", categoryId);
                            startActivity(intent);
                        }
                    }

                } else if (dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.daily_data_menu_t2p))) || dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.title_posm)))) {
                    boolean flag_t2p_mapping;
                    if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P_ADHOC);
                    } else {
                        flag_t2p_mapping = db.isMappingT2PData(store_id, categoryId, CommonString.TABLE_MAPPING_T2P);
                    }
                    if (flag_t2p_mapping) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, T2PComplianceActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                } else if (dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.title_activity_Additional_visibility))) ||
                        dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.title_posm) + "/" + getResources().getString(R.string.daily_data_menu_additional_visibility)))) {
                    Intent intent = new Intent(DailyDataMenuActivity.this, AdditionalVisibility.class);
                    intent.putExtra("categoryName", dailyData.getCategory_name());
                    intent.putExtra("categoryId", categoryId);
                    startActivity(intent);
                    overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                } else if (dailyData.getCategory_name().equalsIgnoreCase((getResources().getString(R.string.daily_data_menu_category_picture)))) {
                    if (camera_allow.equalsIgnoreCase("1")) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, CategoryPicture.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    } else if (country_id != null && !country_id.equals("17")) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, CreateSelfActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.title_activity_ir_image))) {
                    if (isPdIRMapping() != null && !isPdIRMapping().isEmpty() && camera_allow.equals("1")) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, PDimageActivity.class);
                        intent.putExtra("kpi_name", dailyData.getCategory_name());
                        intent.putExtra("categoryName", categoryName);
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                }
                else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.title_activity_ir_visibility))) {
                    if (isPdIRMapping() != null && !isPdIRMapping().isEmpty() && camera_allow.equals("1")) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, PDVisibilityActivity.class);
                        intent.putExtra("kpi_name", dailyData.getCategory_name());
                        intent.putExtra("categoryName", categoryName);
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                }
                else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.order_taking))) {
                    if (isMappingStockData()) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, OrderTakingActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.order_confirm))) {
                    if (isMappingStockData()) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, OrderConfirmActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.daily_data_menu_msl_availability))) {
                    if (isMappingStockData()) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, MSL_Availability_StockFacingActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.stock_entry))) {
                    if (isMappingStockData()) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, StockEntryActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }
                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.daily_data_menu_stock_facing))) {
                    if (isMappingStockData()) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, NewStockDataActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }

                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.audit))) {
                    if (!getAuditQnsRemoved().isEmpty()) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, AuditActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }

                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.pog))) {
                    //add uae pharma
                    if (country_id.equals("2")) {
                        if (camera_allow.equals("1")) {
                            db.open();
                            if (!db.getSubCategoryMasterForPOG(categoryId, storeType_id, pog_type_id, null).isEmpty()) {
                                Intent intent = new Intent(DailyDataMenuActivity.this, POGQuestionsActivity.class);
                                intent.putExtra("categoryName", dailyData.getCategory_name());
                                intent.putExtra("categoryId", categoryId);
                                startActivity(intent);
                                overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                            }

                        }
                    } else {
                        db.open();
                        if (!db.getSubCategoryMasterForPOG(categoryId, storeType_id, pog_type_id, null).isEmpty()) {
                            Intent intent = new Intent(DailyDataMenuActivity.this, POGQuestionsActivity.class);
                            intent.putExtra("categoryName", dailyData.getCategory_name());
                            intent.putExtra("categoryId", categoryId);
                            startActivity(intent);
                            overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                        }

                    }

                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.title_activity_counterfeit_products))) {
                    Intent intent = new Intent(DailyDataMenuActivity.this, CounterfeitProductsActivity.class);
                    intent.putExtra("categoryName", dailyData.getCategory_name());
                    intent.putExtra("categoryId", categoryId);
                    startActivity(intent);
                    overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.stock))) {
                    if (isMappingStockData()) {
                        Intent intent = new Intent(DailyDataMenuActivity.this, StockActivity.class);
                        intent.putExtra("categoryName", dailyData.getCategory_name());
                        intent.putExtra("categoryId", categoryId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                    }

                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.title_activity_stock_pricing))) {
                    Intent intent = new Intent(DailyDataMenuActivity.this, StockPricingActivity.class);
                    intent.putExtra("categoryName", dailyData.getCategory_name());
                    intent.putExtra("categoryId", categoryId);
                    startActivity(intent);
                    overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                } else if (dailyData.getCategory_name().equalsIgnoreCase(getResources().getString(R.string.title_activity_competition_promo))) {
                    Intent intent = new Intent(DailyDataMenuActivity.this, CompetitionPromoActivity.class);
                    intent.putExtra("categoryName", dailyData.getCategory_name());
                    intent.putExtra("categoryId", categoryId);
                    startActivity(intent);
                    overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
                }
            });

        }

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

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView categoryName;
            ImageView categoryIcon;
            LinearLayout lay_menu;
            View irview;
            CardView cardView;

            public MyViewHolder(View itemView) {
                super(itemView);
                categoryName = (TextView) itemView.findViewById(R.id.categoryName);
                categoryIcon = (ImageView) itemView.findViewById(R.id.categoryIcon);
                lay_menu = (LinearLayout) itemView.findViewById(R.id.lay_menu);
                irview =  itemView.findViewById(R.id.irview);
                cardView =  itemView.findViewById(R.id.cardView);
            }
        }
    }

    boolean isMappingStockData() {
        boolean flag;
        if (country_id.equals("7") || country_id.equals("8") || country_id.equals("17")) {
            flag = db.isMappingStockDataStockFacing(categoryId, null, null, null, store_id, CommonString.TABLE_MAPPING_STOCK_STOREWISE);
        } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
            flag = db.isMappingStockDataStockFacing(categoryId, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADHOC);
        } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_ADDITIONAL)) {
            flag = db.isMappingStockDataStockFacing(categoryId, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADDITIONAL);
        } else {
            flag = db.isMappingStockDataStockFacing(categoryId, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK);
        }
        return flag;
    }

    private ArrayList<CategoryPictureGetterSetter> isPdIRMapping() {
        ArrayList<CategoryPictureGetterSetter> ir_list;
        if (country_id.equals("8")) {
            ir_list = db.getCategoryPicturedata(
                    date,
                    categoryId,
                    null,
                    null,
                    null,
                    store_id,
                    CommonString.TABLE_MAPPING_STOCK_STOREWISE
            );
        } else if (store_flag_str != null && store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
            ir_list = db.getCategoryPicturedata(
                    date,
                    categoryId,
                    keyAccount_id,
                    storeType_id,
                    class_id,
                    store_id,
                    CommonString.TABLE_MAPPING_STOCK_ADHOC
            );
        } else {
            ir_list = db.getCategoryPicturedata(
                    date,
                    categoryId,
                    keyAccount_id,
                    storeType_id,
                    class_id,
                    store_id,
                    CommonString.TABLE_MAPPING_STOCK
            );
        }

        return ir_list;
    }

    private ArrayList<CategoryPictureGetterSetter> isPdVisibiltyMapping() {
        ArrayList<CategoryPictureGetterSetter> ir_list;
        if (country_id.equals("6")) {
            ir_list = db.getirVisibilityData(date,store_id,categoryId);
        } else if (store_flag_str != null && store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
            ir_list = db.getirVisibilityData(date,store_id,categoryId);
        } else {
            ir_list = db.getirVisibilityData(date,store_id,categoryId);
        }
        return ir_list;
    }

}

