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 java.util.Objects;

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, "");
        preferences.getString(CommonString.KEY_VISIT_ID, "");
        visit_type_id = preferences.getString(CommonString.KEY_VISIT_ID, "");
        toolbar.setTitle(getResources().getString(R.string.title_activity_category_list));
        setSupportActionBar(toolbar);
        Objects.requireNonNull(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");
        } 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++) {
                String category_id = categoryList.get(i).getCategory_id();
                boolean flag_filled = isCategoryComplete(category_id);
                int imageRes = getImageRes(flag_filled, category_id);
                categoryList.get(i).setCategory_img(imageRes);
            }

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


    }

    private int getImageRes(boolean flag_filled, String category_id) {
        int imageRes = 0;
        if (flag_filled) {
            imageRes = switch (category_id) {
                case "1" -> R.mipmap.health_food_drink_tick;
                case "2" -> R.mipmap.oralcare_tick;
                case "3" -> R.mipmap.wellness_tick;
                case "4" -> R.mipmap.nutritionals_tick;
                case "5" -> R.mipmap.gastro_n_indigestion_tick;
                case "6", "10" -> R.mipmap.cough_n_cold_tick;
                case "7", "9" -> R.mipmap.analgesics_tick;
                case "8" -> R.mipmap.first_aid_tick;
                default -> imageRes;
            };
        } else {
            imageRes = switch (category_id) {
                case "1" -> R.mipmap.health_food_drink;
                case "2" -> R.mipmap.oral_care;
                case "3" -> R.mipmap.wellness;
                case "4" -> R.mipmap.nutritionals;
                case "5" -> R.mipmap.gastro_n_indigestion;
                case "6", "10" -> R.mipmap.cough_n_cold;
                case "7", "9" -> R.mipmap.analgesics;
                case "8" -> R.mipmap.first_aid;
                default -> imageRes;
            };
        }
        return imageRes;
    }

    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());
            Log.e("CatImg--", "" + categoryData.getCategory_img());
            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;
                        }
                    }

                    ArrayList<CategoryPictureGetterSetter> irVisibilityList = isPdVisibiltyMapping(category_id);
                    if (irVisibilityList != null && !irVisibilityList.isEmpty() && camera_allow.equals("1")) {
                        if (!db.isPdVisibility(store_id, visit_date, CommonString.KEY_COMPELETE, category_id)) {
                            flag_filled = false;
                            break;
                        }
                    }

                    flag_filled = true;
                } else {
                    flag_filled = false;
                    break;
                }
            }
        }
        db.open();
        db.updateCheckoutStatus(store_id, flag_filled ? CommonString.KEY_VALID : CommonString.KEY_INVALID, 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;
    }

    private ArrayList<CategoryPictureGetterSetter> isPdVisibiltyMapping(String categoryId) {
        ArrayList<CategoryPictureGetterSetter> ir_list;
        if (country_id.equals("8")) {
            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;
    }

    private boolean isCategoryComplete(String category_id) {
        boolean flag_filled = true;

        if (store_flag_str.equalsIgnoreCase(CommonString.FROM_ADDITIONAL)) {
            flag_filled = db.isMappingStockDataStockFacing(category_id, keyAccount_id, storeType_id, class_id, store_id,
                    CommonString.TABLE_MAPPING_STOCK_ADDITIONAL)
                    && db.checkStockData(store_id, category_id, visit_date);
        } 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)) {
                return false;
            }
            // MSL & Stock Entry
            if (!validateStk(category_id)) {
                return false;
            }

            // T2P
            if (db.isMappingT2PData(store_id, category_id,
                    store_flag_str.equals(CommonString.FROM_DEVIATION)
                            ? CommonString.TABLE_MAPPING_T2P_ADHOC
                            : CommonString.TABLE_MAPPING_T2P)) {
                if (!db.isFilledT2P(store_id, category_id)) {
                    return false;
                }
            }

            // Camera
            if (camera_allow.equals("1")) {
                if (!db.isCategoryPictureData(store_id, category_id)) {
                    return false;
                }
            } else {
                ArrayList<MSL_AvailabilityStockFacingGetterSetter> sub_category_list =
                        db.getSubCategoryMaster(category_id, store_id, country_id);

                boolean isSubGroupMissing = false;
                for (MSL_AvailabilityStockFacingGetterSetter subCat : sub_category_list) {
                    if (db.getSkuGroupMasterData(category_id, subCat.getSub_category_id()).isEmpty()) {
                        isSubGroupMissing = true;
                        break;
                    }
                }

                if (isSubGroupMissing && db.getNoCameraCategoryDataInserted(store_id, category_id, null).isEmpty()) {
                    return false;
                }
            }

            // Audit Questions
            if (!getAuditQnsRemoved(category_id).isEmpty()
                    && db.getAfterSaveAuditQuestionAnswerData(store_id, category_id).isEmpty()) {
                return false;
            }

            // POG Upload
            if (!db.getSubCategoryMasterForPOG(category_id, storeType_id, pog_type_id, null).isEmpty()
                    && db.getAfterSavePOGUploadQuestionAnswerData(store_id, category_id).isEmpty()) {
                return false;
            }

            // Egypt - Counterfeit & Promo
            if (country_id.equals("6")) {
                if (db.getCounterfeitProductExistsAfterData(store_id, category_id).isEmpty()) {
                    return false;
                }
                if (category_id.equals("2") && db.getCompetitionPromoExistsAfterData(store_id, category_id).isEmpty()) {
                    return false;
                }
            }

            // Order Taking for country 15
            if (country_id.equals("15") && !db.checkOrder_taking_Data(store_id, category_id)) {
                return false;
            }

            // IR-PD Image Check
            ArrayList<CategoryPictureGetterSetter> irList = isPdIRMapping(category_id);
            if (irList != null && !irList.isEmpty() && camera_allow.equals("1")) {
                if (!db.isFilledIRPDdata(store_id, visit_date, category_id)) {
                    return false;
                }
            }

            ArrayList<CategoryPictureGetterSetter> irvisiList = isPdVisibiltyMapping(category_id);
            if (irvisiList != null && !irvisiList.isEmpty() && camera_allow.equals("1")) {
                if (!db.isPdVisibility(store_id, visit_date, CommonString.KEY_COMPELETE, category_id)) {
                    return false;
                }
            }
        }
        return flag_filled;
    }


    private boolean validateStk(String category_id) {
        boolean flag_filled = true;
        if (isMappingStockData(category_id)) {
            if (country_id.equals("10")) {
                db.open();
                if (!db.checkStock_Entry_FacingDataNew(store_id, category_id, visit_date)) {
                    flag_filled = false;
                }
            } else {
                if (!db.checkMsl_Availability_StockFacingDataNew(store_id, category_id, visit_date)) {
                    flag_filled = false;
                }
            }
        }
        return flag_filled;
    }

    boolean isMappingStockData(String categoryId) {
        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 {
            flag = db.isMappingStockDataStockFacing(categoryId, keyAccount_id, storeType_id, class_id, store_id, CommonString.TABLE_MAPPING_STOCK);
        }
        return flag;
    }
}
