package cpm.com.gskmtorange.gsk_dailyentry;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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 androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

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

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.constant.CommonFunctions;
import cpm.com.gskmtorange.constant.CommonString;
import cpm.com.gskmtorange.xmlGetterSetter.AuditDataGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.CategoryGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.MSL_AvailabilityStockFacingGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.T2PGetterSetter;

public class CategoryListActivity extends AppCompatActivity {
    private Context context;
    RecyclerView recyclerView;
    ArrayList<CategoryGetterSetter> categoryList;
    CategoryListAdapter adapter;
    GSKOrangeDB db;
    String store_id, visit_date, username, intime, date, keyAccount_id, class_id, storeType_id, camera_allow, country_id, store_flag_str, pog_type_id;
    private SharedPreferences preferences;
    String visit_type_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        db = new GSKOrangeDB(this);
        db.open();
        context = this;
        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, null);
        store_flag_str = preferences.getString(CommonString.KEY_STORE_FLAG, null);
        pog_type_id = preferences.getString(CommonString.KEY_POG_TYPE_ID, "");
        if (preferences.getString(CommonString.KEY_VISIT_ID, "") != null) {
            visit_type_id = preferences.getString(CommonString.KEY_VISIT_ID, "");
        }
        toolbar.setTitle(getResources().getString(R.string.title_activity_category_list));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_category);
        fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show());
    }

    @Override
    protected void onResume() {
        super.onResume();
        CommonFunctions.updateLangResources(context, preferences.getString(CommonString.KEY_LANGUAGE, ""));
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        categoryList = new ArrayList<>();
        Log.e("country_data", country_id + "," + keyAccount_id);
        if (country_id.equals("7") || country_id.equals("8") || country_id.equals("17")) {
            categoryList = db.getCategoryListData(null, null, null, store_id, "MAPPING_STOCK_STOREWISE");
            Log.d("catlistsize", String.valueOf(categoryList.size()));
        } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
            categoryList = db.getCategoryListData(keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADHOC);
        } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_ADDITIONAL)) {
            categoryList = db.getCategoryListData(keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADDITIONAL);
        } else {
            categoryList = db.getCategoryListData(keyAccount_id, storeType_id, class_id, store_id, "MAPPING_STOCK");
        }
        if (categoryList != null && !categoryList.isEmpty()) {
            for (int i = 0; i < categoryList.size(); i++) {
                boolean flag_filled = false;
                String category_id = categoryList.get(i).getCategory_id();
                if (store_flag_str.equalsIgnoreCase(CommonString.FROM_ADDITIONAL)) {
                    db.open();
                    boolean flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADDITIONAL);
                    if (flag_mapping_stock) {
                        if (db.checkStockData(store_id, category_id, visit_date)) {
                            flag_filled = true;
                        } else {
                            flag_filled = false;
                        }
                    }
                } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION) && country_id.equals("2") && visit_type_id.equals("3")) {
                    db.open();
                    if (db.additionalVisibilitydata(store_id, category_id)) {
                        flag_filled = true;
                    } else {
                        flag_filled = false;
                    }
                } else {
                    //Additional_Visibility
                    if (db.additionalVisibilitydata(store_id, category_id)) {
                        boolean flag = true;
                        //MSL_Availability
                        boolean flag_mapping_stock;
                        //New MSL_Availability_StockFacing
                        if (country_id.equals("7")) {
                            flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, null, null, null, store_id, CommonString.TABLE_MAPPING_STOCK_STOREWISE);
                        } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                            flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADHOC);
                        } else {
                            flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK);
                        }
                        if (flag_mapping_stock) {
                            if (!db.checkMsl_Availability_StockFacingDataNew(store_id, category_id, visit_date)) {
                                flag = false;
                            }
                        }

                        //Stock_Entry
                        boolean flag_stock_mapping_stock;
                        if (country_id.equals("10")) {
                            flag_stock_mapping_stock = db.isMappingStockDataStockFacing(category_id, null, null, null, store_id, CommonString.TABLE_MAPPING_STOCK_STOREWISE);
                        } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                            flag_stock_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADHOC);
                        } else {
                            flag_stock_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK);
                        }
                        if (flag_stock_mapping_stock) {
                            if (!db.checkStock_Entry_FacingDataNew(store_id, category_id, visit_date)) {
                                flag = false;
                            }
                        }

                        //Promo Compliance
                        if (flag) {
                            if (country_id.equals("7")) {
                                if (db.getPromoComplianceSkuwiseExistsAfterData(store_id, category_id).size() == 0) {
                                    flag = false;
                                }
                            } else {
                                boolean isMappingPromotion_Flag;
                                if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                                    isMappingPromotion_Flag = db.isMappingPromotionData(store_id, category_id, CommonString.TABLE_MAPPING_PROMOTION_ADHOC);
                                } else {
                                    isMappingPromotion_Flag = db.isMappingPromotionData(store_id, category_id, CommonString.TABLE_MAPPING_PROMOTION);
                                }
                                if (isMappingPromotion_Flag) {
                                    if (!db.checkPromoComplianceData(store_id, category_id)) {
                                        flag = false;
                                    }
                                }
                            }

                        }

                        //T2P
                        if (flag) {
                            boolean flag_t2p_mapping;
                            if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                                flag_t2p_mapping = db.isMappingT2PData(store_id, category_id, CommonString.TABLE_MAPPING_T2P_ADHOC);
                            } else {
                                flag_t2p_mapping = db.isMappingT2PData(store_id, category_id, CommonString.TABLE_MAPPING_T2P);
                            }
                            if (flag_t2p_mapping) {
                                if (!db.isFilledT2P(store_id, category_id)) {
                                    flag = false;
                                }
                            }
                        }

                        //Category image
                        if (camera_allow.equals("1")) {
                            if (!db.isCategoryPictureData(store_id, category_id)) {
                                flag = false;
                            }
                        } else {
                            final ArrayList<MSL_AvailabilityStockFacingGetterSetter> sub_category_list = db.getSubCategoryMaster(category_id, store_id, country_id);
                            //if Sub Group Data exists for any Sub Category of the Category
                            boolean isSubGroupDataExists = false;
                            for (int j = 0; j < sub_category_list.size(); j++) {
                                if (db.getSkuGroupMasterData(category_id, sub_category_list.get(j).getSub_category_id()).size() > 0) {
                                    isSubGroupDataExists = true;
                                    break;
                                }
                            }

                            if (isSubGroupDataExists) {
                                if (db.getNoCameraCategoryDataInserted(store_id, category_id, null).size() == 0) {
                                    flag = false;
                                }
                            }
                        }

                        //Order Taking Data
                        if (country_id.equals("15") || ((country_id.equals("17") && category_id.equals("2")))) {
                            if (!db.checkOrder_taking_Data(store_id, category_id)) {
                                flag = false;
                            }
                        }

                        if (flag) {
                            //changed on 23.10.2018
                            //ArrayList<AuditDataGetterSetter> question_list = getAuditAfterDeletion(category_id);
                            //changed on 03.12.2018
                            ArrayList<AuditDataGetterSetter> question_list = getAuditQnsRemoved(category_id);
                            if (question_list.size() > 0) {
                                /*if (!country_id.equals("6") && db.getAfterSaveAuditQuestionAnswerData(store_id, category_id).size() == 0) {
                                    flag = false;
                                }*/
                                if (db.getAfterSaveAuditQuestionAnswerData(store_id, category_id).size() == 0) {
                                    flag = false;
                                }
                            }
                        }

                        //new add UAE pharma
                        if (flag) {
                            if (db.getSubCategoryMasterForPOG(category_id, storeType_id, pog_type_id, null).size() > 0) {
                                if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, category_id).size() == 0) {
                                    flag = false;
                                }
                            }
                            if (country_id.equals("2")) {
                                if (camera_allow.equals("1")) {
                                    if (db.getSubCategoryMasterForPOG(category_id, storeType_id, pog_type_id, null).size() > 0) {
                                        if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, category_id).size() == 0) {
                                            flag = false;
                                        }
                                    }
                                } else {
                                    flag = true;
                                }
                            } else {
                                if (db.getSubCategoryMasterForPOG(category_id, storeType_id, pog_type_id, null).size() > 0) {
                                    if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, category_id).size() == 0) {
                                        flag = false;
                                    }
                                }
                            }
                        }

                        if (flag) {
                            //Counterfeit Product Option - Egypt
                            if (country_id.equals("6")) {
                                if (db.getCounterfeitProductExistsAfterData(store_id, category_id).size() == 0) {
                                    flag = false;
                                }

                            }
                            //usk
                            if (country_id.equals("6") && category_id.equals("2")) {
                                if (db.getCompetitionPromoExistsAfterData(store_id, category_id).size() == 0) {
                                    flag = false;
                                }
                            }
                        }

                        //// IR PD images handling by jp
                        ArrayList<CategoryPictureGetterSetter> irList = isPdIRMapping(category_id);
                        if (irList != null && !irList.isEmpty() && camera_allow.equals("1")) {
                            //flag = db.isFilledIRPDdata(store_id, visit_date, category_id);
                           // flag = db.isSOS(store_id, date, CommonString.KEY_COMPELETE);
                            flag = db.isSOSCategory(store_id, visit_date, CommonString.KEY_COMPELETE, category_id);
                        } else {
                            flag = true;
                        }
                        flag_filled = flag;
                    }
                }

                if (flag_filled) {
                    switch (category_id) {
                        case "1":
                            categoryList.get(i).setCategory_img(R.mipmap.health_food_drink_tick);
                            break;
                        case "2":
                            categoryList.get(i).setCategory_img(R.mipmap.oralcare_tick);
                            break;
                        case "3":
                            categoryList.get(i).setCategory_img(R.mipmap.wellness_tick);
                            break;
                        case "4":
                            categoryList.get(i).setCategory_img(R.mipmap.nutritionals_tick);
                            break;
                        case "5":
                            categoryList.get(i).setCategory_img(R.mipmap.gastro_n_indigestion_tick);
                            break;
                        case "6":
                        case "10":
                            categoryList.get(i).setCategory_img(R.mipmap.cough_n_cold_tick);
                            break;
                        case "7":
                        case "9":
                            categoryList.get(i).setCategory_img(R.mipmap.analgesics_tick);
                            break;
                        case "8":
                            categoryList.get(i).setCategory_img(R.mipmap.first_aid_tick);
                            break;
                    }
                } else {
                    switch (category_id) {
                        case "1":
                            categoryList.get(i).setCategory_img(R.mipmap.health_food_drink);
                            break;
                        case "2":
                            categoryList.get(i).setCategory_img(R.mipmap.oral_care);
                            break;
                        case "3":
                            categoryList.get(i).setCategory_img(R.mipmap.wellness);
                            break;
                        case "4":
                            categoryList.get(i).setCategory_img(R.mipmap.nutritionals);
                            break;
                        case "5":
                            categoryList.get(i).setCategory_img(R.mipmap.gastro_n_indigestion);
                            break;
                        case "6":
                        case "10":
                            categoryList.get(i).setCategory_img(R.mipmap.cough_n_cold);
                            break;
                        case "7":
                        case "9":
                            categoryList.get(i).setCategory_img(R.mipmap.analgesics);
                            break;
                        case "8":
                            categoryList.get(i).setCategory_img(R.mipmap.first_aid);
                            break;
                    }
                }
            }

            adapter = new CategoryListAdapter(CategoryListActivity.this, categoryList);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new GridLayoutManager(context, 2));
            updateStatus();
        }

    }

    ArrayList<AuditDataGetterSetter> getAuditAfterDeletion(String category_id) {
        ArrayList<AuditDataGetterSetter> question_list = db.getAuditCategoryWise(category_id, storeType_id);

        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")) {
                boolean flag_t2p_mapping;
                if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                    flag_t2p_mapping = db.isMappingT2PData(store_id, category_id, CommonString.TABLE_MAPPING_T2P_ADHOC);
                } else {
                    flag_t2p_mapping = db.isMappingT2PData(store_id, category_id, CommonString.TABLE_MAPPING_T2P);
                }
                if (!flag_t2p_mapping) {
                    iterator.remove();
                }
            }
        }

        return question_list;
    }

    ArrayList<AuditDataGetterSetter> getAuditQnsRemoved(String categoryId) {
        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) {
            db.open();
            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("questionlistsize", String.valueOf(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();
                }
            }
        }

        return question_list;
    }

    @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();
            overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
        }

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

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
    }

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

        public CategoryListAdapter(CategoryListActivity context, List<CategoryGetterSetter> list) {
            inflator = LayoutInflater.from(context);
            this.list = list;
            this.context = context;
        }

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

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final CategoryGetterSetter categoryData = list.get(position);
            holder.categoryName.setText(categoryData.getCategory());
            holder.categoryIcon.setImageResource(categoryData.getCategory_img());
            holder.lay_menu.setOnClickListener(view -> {
                Intent intent = new Intent(CategoryListActivity.this, CategoryWisePerformanceActivity.class);
                intent.putExtra("categoryName", categoryData.getCategory());
                intent.putExtra("categoryId", categoryData.getCategory_id());
                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;

            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);
            }
        }
    }

    public void updateStatus() {
        //Update Checkout Status
        boolean flag_filled = false;
        for (int i = 0; i < categoryList.size(); i++) {
            String category_id = categoryList.get(i).getCategory_id();
            if (store_flag_str.equalsIgnoreCase(CommonString.FROM_ADDITIONAL)) {
                boolean flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADDITIONAL);
                if (flag_mapping_stock) {
                    if (db.checkStockData(store_id, category_id, visit_date)) {
                        flag_filled = true;
                    } else {
                        flag_filled = false;
                        break;
                    }
                }
            } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION) && country_id.equals("2") && visit_type_id.equals("3")) {
                db.open();
                flag_filled = db.additionalVisibilitydata(store_id, category_id);
            } else {
                db.open();
                if (db.additionalVisibilitydata(store_id, category_id)) {
                    boolean flag_mapping_stock;
                    //New MSL_Availability_StockFacing
                    if (country_id.equals("7") || country_id.equals("8")) {
                        flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, null, null, null, store_id, CommonString.TABLE_MAPPING_STOCK_STOREWISE);
                    } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADHOC);
                    } else {
                        flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK);
                    }
                    if (flag_mapping_stock) {
                        if (!db.checkMsl_Availability_StockFacingDataNew(store_id, category_id, visit_date)) {
                            flag_filled = false;
                            break;
                        }
                    }

                    //New MSL_Availability_StockFacing
                    if (country_id.equals("10") || country_id.equals("8")) {
                        flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, null, null, null, store_id, CommonString.TABLE_MAPPING_STOCK_STOREWISE);
                    } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADHOC);
                    } else {
                        flag_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK);
                    }
                    if (flag_mapping_stock) {
                        if (!db.checkMsl_Availability_StockFacingDataNew(store_id, category_id, visit_date)) {
                            flag_filled = false;
                            break;
                        }
                    }

                    //Stock_Entry
                    boolean flag_stock_mapping_stock;
                    if (country_id.equals("10")) {
                        flag_stock_mapping_stock = db.isMappingStockDataStockFacing(category_id, null, null, null, store_id, CommonString.TABLE_MAPPING_STOCK_STOREWISE);
                    } else if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        flag_stock_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK_ADHOC);
                    } else {
                        flag_stock_mapping_stock = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK);
                    }
                    if (flag_stock_mapping_stock) {
                        if (!db.checkStock_Entry_FacingDataNew(store_id, category_id, visit_date)) {
                            flag_filled = false;
                        }
                    }

                    if (country_id.equals("7")) {
                        if (db.getPromoComplianceSkuwiseExistsAfterData(store_id, category_id).isEmpty()) {
                            flag_filled = false;
                            break;
                        }
                    } else {
                        boolean isMappingPromotion_Flag;
                        if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                            isMappingPromotion_Flag = db.isMappingPromotionData(store_id, category_id, CommonString.TABLE_MAPPING_PROMOTION_ADHOC);
                        } else {
                            isMappingPromotion_Flag = db.isMappingPromotionData(store_id, category_id, CommonString.TABLE_MAPPING_PROMOTION);
                        }
                        if (isMappingPromotion_Flag) {
                            if (!db.checkPromoComplianceData(store_id, category_id)) {
                                flag_filled = false;
                                break;
                            }
                        }
                    }

                    boolean flag_t2p_mapping;
                    if (store_flag_str.equalsIgnoreCase(CommonString.FROM_DEVIATION)) {
                        flag_t2p_mapping = db.isMappingT2PData(store_id, category_id, CommonString.TABLE_MAPPING_T2P_ADHOC);
                    } else {
                        flag_t2p_mapping = db.isMappingT2PData(store_id, category_id, CommonString.TABLE_MAPPING_T2P);
                    }
                    if (flag_t2p_mapping) {
                        if (!db.isFilledT2P(store_id, category_id)) {
                            flag_filled = false;
                            break;
                        }
                    }

                    if (camera_allow.equals("1")) {
                        db.open();
                        if (!db.isCategoryPictureData(store_id, category_id)) {
                            flag_filled = false;
                            break;
                        }
                    } else {
                        final ArrayList<MSL_AvailabilityStockFacingGetterSetter> sub_category_list = db.getSubCategoryMaster(category_id, store_id, country_id);
                        //if Sub Group Data exists for any Sub Category of the Category
                        boolean isSubGroupDataExists = false;
                        for (int j = 0; j < sub_category_list.size(); j++) {
                            if (!db.getSkuGroupMasterData(category_id, sub_category_list.get(j).getSub_category_id()).isEmpty()) {
                                isSubGroupDataExists = true;
                                break;
                            }
                        }

                        if (isSubGroupDataExists) {
                            if (db.getNoCameraCategoryDataInserted(store_id, category_id, null).isEmpty()) {
                                flag_filled = false;
                                break;
                            }
                        }
                    }

                    if (!getAuditQnsRemoved(category_id).isEmpty()) {
                        ///Make non Mandate Audit KPI for EGIPT
                        //Make Mandate Audit KPI FOR EGYPT 17-01-2023 BY VIKAS
                        if (db.getAfterSaveAuditQuestionAnswerData(store_id, category_id).isEmpty()) {
                            flag_filled = false;
                            break;
                        }
                    }

                    if (country_id.equals("2")) {
                        if (camera_allow.equals("1")) {
                            if (!db.getSubCategoryMasterForPOG(category_id, storeType_id, pog_type_id, null).isEmpty()) {
                                if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, category_id).isEmpty()) {
                                    flag_filled = false;
                                    break;
                                }
                            }
                        }
                    } else {
                        if (!db.getSubCategoryMasterForPOG(category_id, storeType_id, pog_type_id, null).isEmpty()) {
                            if (db.getAfterSavePOGUploadQuestionAnswerData(store_id, category_id).isEmpty()) {
                                flag_filled = false;
                                break;
                            }
                        }
                    }

                    //Counterfeit Product Option - Egypt
                    if (country_id.equals("6")) {
                        if (db.getCounterfeitProductExistsAfterData(store_id, category_id).isEmpty()) {
                            flag_filled = false;
                            break;
                        }

                    }
                    //usk
                    if (country_id.equals("6") && category_id.equals("2")) {
                        if (db.getCompetitionPromoExistsAfterData(store_id, category_id).isEmpty()) {
                            flag_filled = false;
                            break;
                        }
                    }
                    if (country_id.equals("15")) {
                        if (!db.checkOrder_taking_Data(store_id, category_id)) {
                            flag_filled = false;
                            break;
                        }
                    }

                    //// IR PD images handling by jp
                    ArrayList<CategoryPictureGetterSetter> irList = isPdIRMapping(category_id);
                    if (irList != null && !irList.isEmpty() && camera_allow.equals("1")) {
                        if (!db.isFilledIRPDdata(store_id, visit_date, category_id)) {
                            flag_filled = false;
                            break;
                        }
                    }

                    flag_filled = true;
                } else {
                    flag_filled = false;
                    break;
                }
            }
        }
        if (flag_filled) {
            db.updateCheckoutStatus(store_id, CommonString.KEY_VALID, CommonString.KEY_JOURNEY_PLAN);
        }
    }


    private ArrayList<CategoryPictureGetterSetter> isPdIRMapping(String categoryId) {
        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;
    }
}
