package cpm.com.gskmtorange.pharma_stores;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;

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

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cpm.com.gskmtorange.Database.GSKOrangeDB;
import cpm.com.gskmtorange.R;
import cpm.com.gskmtorange.constant.CommonFunctions;
import cpm.com.gskmtorange.constant.CommonString;
import cpm.com.gskmtorange.dailyentry.StockActivity;
import cpm.com.gskmtorange.xmlGetterSetter.MSL_AvailabilityStockFacingGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.MSL_AvailabilityStockFacingGetterSetter;
import cpm.com.gskmtorange.xmlGetterSetter.PharmaMSLInsertedGetterSetter;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MSLFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MSLFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MSLFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String CategoryName = "categoryName";
    private static final String CategoryId = "categoryId";

    private OnFragmentInteractionListener mListener;

    GSKOrangeDB db;

    String categoryName, categoryId, storeId, Error_Message = "";
    String store_id, visit_date, username, intime, date, keyAccount_id, class_id, storeType_id, country_id, store_flag_str;
    boolean isDialogOpen = true;
    private SharedPreferences preferences;
    int scrollPosition = 0;

    ExpandableListView expandableListView;
    ExpandableListAdapter adapter;

    ArrayList<MSL_AvailabilityStockFacingGetterSetter> headerDataList;
    ArrayList<PharmaMSLInsertedGetterSetter> childDataList;
    List<MSL_AvailabilityStockFacingGetterSetter> hashMapListHeaderData;
    HashMap<MSL_AvailabilityStockFacingGetterSetter, List<PharmaMSLInsertedGetterSetter>> hashMapListChildData;

    List<Integer> checkHeaderArray = new ArrayList<>();
    boolean checkflag = true;
    FloatingActionButton fab;
    boolean editFlag = false;

    String gallery_package = "";
    Uri outputFileUri;

    String str = CommonString.getImagesFolder(getContext()), path = "", _pathforcheck = "";

    int child_position = -1;
    int group_position = -1;

    public MSLFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MSLFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MSLFragment newInstance(String param1, String param2) {
        MSLFragment fragment = new MSLFragment();
        Bundle args = new Bundle();
        args.putString(CategoryName, param1);
        args.putString(CategoryId, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            categoryName = getArguments().getString(CategoryName);
            categoryId = getArguments().getString(CategoryId);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msl, container, false);
        // Inflate the layout for this fragment

        expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView);
        fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);

        preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        CommonFunctions.updateLangResources(getActivity(), 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, "");
        country_id = preferences.getString(CommonString.KEY_COUNTRY_ID, "");
        store_flag_str = preferences.getString(CommonString.KEY_STORE_FLAG, null);

        expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView);

        getActivity().setTitle(R.string.msl_availability_availability);


        db = new GSKOrangeDB(getActivity());
        db.open();

        prepareList();

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(validateData(headerDataList, hashMapListChildData)){
                    db.InsertPharmaMSL(store_id, headerDataList, hashMapListChildData);

                    //update status to valid
                    db.updateCheckoutStatus(store_id, CommonString.KEY_VALID, CommonString.KEY_JOURNEY_PLAN_PHARMA);

                    onButtonPressed();
                }
                else {
                    for (int i = 0; i < adapter.getGroupCount(); i++)
                        expandableListView.expandGroup(i);

                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage(Error_Message)
                            .setCancelable(false)
                            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    expandableListView.smoothScrollToPosition(scrollPosition);

                                    dialog.dismiss();
                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();
                }
            }
        });

        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed() {
        if (mListener != null) {
            mListener.onFragmentInteraction(getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment));
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Fragment uri);
    }

    private void prepareList() {
        try {
            hashMapListHeaderData = new ArrayList<>();
            hashMapListChildData = new HashMap<>();

            //Header
            headerDataList = db.getPharmaMSLHeaderData();

            if (headerDataList.size() > 0) {
                for (int i = 0; i < headerDataList.size(); i++) {
                    hashMapListHeaderData.add(headerDataList.get(i));

                    childDataList = new ArrayList<>();
                    //childDataList = db.getStockAfterSaveData(categoryId, headerDataList.get(i).getBrand_id(), store_id);
                    if (childDataList.size() == 0) {
                        childDataList = db.getPharmaMSLSKUData(headerDataList.get(i).getBrand_id());
                    }

                    hashMapListChildData.put(hashMapListHeaderData.get(i), childDataList);
                }
            }

            adapter = new ExpandableListAdapter(getActivity(), hashMapListHeaderData, hashMapListChildData);
            expandableListView.setAdapter(adapter);

            expandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                    expandableListView.invalidate();

                    int lastItem = firstVisibleItem + visibleItemCount;

                    if (firstVisibleItem == 0) {
                        fab.show();//setVisibility(View.VISIBLE);
                    } else if (lastItem == totalItemCount) {
                        fab.hide();//setVisibility(View.INVISIBLE);
                    } else {
                        fab.show();//setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onScrollStateChanged(AbsListView arg0, int arg1) {
                    InputMethodManager inputManager = (InputMethodManager) getActivity()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (getActivity().getCurrentFocus() != null) {
                        inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                        getActivity().getCurrentFocus().clearFocus();
                    }

                    expandableListView.invalidateViews();
                    //adapter.notifyDataSetChanged();
                }
            });

            // Listview Group click listener
            expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                    return false;
                }
            });

            // Listview Group expanded listener
            expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int groupPosition) {
                    InputMethodManager inputManager = (InputMethodManager) getActivity()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (getActivity().getWindow().getCurrentFocus() != null) {
                        inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                        getActivity().getCurrentFocus().clearFocus();
                    }
                }
            });

            // Listview Group collasped listener
            expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
                @Override
                public void onGroupCollapse(int groupPosition) {
                    InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (getActivity().getWindow().getCurrentFocus() != null) {
                        inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                        getActivity().getCurrentFocus().clearFocus();
                    }
                }
            });

            // Listview on child click listener
            expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                            int childPosition, long id) {
                    return false;
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {
        private Context _context;
        private List<MSL_AvailabilityStockFacingGetterSetter> _listDataHeader;
        private HashMap<MSL_AvailabilityStockFacingGetterSetter, List<PharmaMSLInsertedGetterSetter>> _listDataChild;

        public ExpandableListAdapter(Context context, List<MSL_AvailabilityStockFacingGetterSetter> listDataHeader,
                                     HashMap<MSL_AvailabilityStockFacingGetterSetter, List<PharmaMSLInsertedGetterSetter>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return this._listDataHeader.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return this._listDataHeader.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            MSL_AvailabilityStockFacingGetterSetter headerTitle = (MSL_AvailabilityStockFacingGetterSetter) getGroup(groupPosition);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.item_msl_availability_stock_facing_header, null, false);
            }

            TextView txt_categoryHeader = (TextView) convertView.findViewById(R.id.txt_categoryHeader);
            RelativeLayout rel_header = (RelativeLayout) convertView.findViewById(R.id.rel_categoryHeader);
            ImageView img_camera = (ImageView) convertView.findViewById(R.id.img_camera);
            CardView card_view = (CardView) convertView.findViewById(R.id.card_view);

            txt_categoryHeader.setTypeface(null, Typeface.BOLD);

            if (headerTitle.getCompany_id().equals("1")) {
                txt_categoryHeader.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
            } else {
                txt_categoryHeader.setTextColor(getResources().getColor(R.color.black));
            }
            txt_categoryHeader.setText(headerTitle.getSub_category() + "-" + headerTitle.getBrand());


            if (!checkflag) {
                if (checkHeaderArray.contains(groupPosition)) {
                    card_view.setCardBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
                } else {
                    card_view.setCardBackgroundColor(getResources().getColor(R.color.grey_background));
                }
            }

            //empty check color change
            /*if (headerTitle.getCompany_id().equals("1")) {
                if (!checkflag) {
                    if (checkHeaderArray.contains(groupPosition)) {
                        //card_view.setCardBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
                        txt_categoryHeader.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                    } else {
                        txt_categoryHeader.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                    }
                }
            } else {
                if (!checkflag) {
                    if (checkHeaderArray.contains(groupPosition)) {
                        txt_categoryHeader.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                    } else {
                        txt_categoryHeader.setTextColor(getResources().getColor(R.color.black));
                    }
                }
            }*/

            return convertView;
        }

        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
                                 View convertView, ViewGroup parent) {

            final PharmaMSLInsertedGetterSetter childData =
                    (PharmaMSLInsertedGetterSetter) getChild(groupPosition, childPosition);
            ViewHolder holder = null;

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.pharma_msl_item, null, false);

                holder = new ViewHolder();
                holder.cardView = (CardView) convertView.findViewById(R.id.card_view);
                holder.lin_category = (LinearLayout) convertView.findViewById(R.id.lin_category);

                holder.txt_skuName = (TextView) convertView.findViewById(R.id.txt_skuName);

                holder.rg = (RadioGroup) convertView.findViewById(R.id.radio_group);
                holder.radio_yes = (RadioButton) convertView.findViewById(R.id.radio_yes);
                holder.radio_no = (RadioButton) convertView.findViewById(R.id.radio_no);
                holder.img_cam = (ImageView) convertView.findViewById(R.id.img_cam);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            //hidden for MSL
            holder.img_cam.setVisibility(View.GONE);

            holder.txt_skuName.setText(childData.getSku());

           /* if (childData.getCompany_id().equals("1") && childData.getMust_have().equals("1")) {
                holder.lin_category.setBackgroundColor(getResources().getColor(R.color.colorOrange));

            } else {

                holder.lin_category.setBackgroundColor(getResources().getColor(R.color.white));

            }*/

            holder.lin_category.setBackgroundColor(getResources().getColor(R.color.white));

            /*if (childData.getCompany_id().equals("1")) {

                if (childData.getMust_have().equals("1")) {
                    holder.txt_skuName.setTextColor(getResources().getColor(R.color.colorPrimary));
                } else {
                    holder.txt_skuName.setTextColor(getResources().getColor(R.color.blue));
                }

            } else {
                holder.txt_skuName.setTextColor(getResources().getColor(R.color.black));
            }
*/

            final ViewHolder finalHolder1 = holder;
            holder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if (finalHolder1.radio_yes.isChecked()) {

                        finalHolder1.radio_yes.setBackground(getActivity().getResources().getDrawable(R.mipmap.yes_green));
                        finalHolder1.radio_no.setBackground(getActivity().getResources().getDrawable(R.mipmap.no_white));
                        //finalHolder1.img_cam.setVisibility(View.VISIBLE);

                        childData.setPresent(1);
                        //adapter.notifyDataSetChanged();
                    }
                    if (finalHolder1.radio_no.isChecked()){

                        finalHolder1.radio_no.setBackground(getActivity().getResources().getDrawable(R.mipmap.no_red));
                        finalHolder1.radio_yes.setBackground(getActivity().getResources().getDrawable(R.mipmap.yes_white));
                        //finalHolder1.img_cam.setVisibility(View.GONE);

                        childData.setPresent(0);
                        //adapter.notifyDataSetChanged();
                    }
                }
            });

                       //Camera
           /* holder.img_cam.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    _pathforcheck = "Pharma_MSL_Image_" + store_id + categoryId + "_" + childData.getBrand_id() + visit_date.replace("/", "") + "_" +
                            CommonFunctions.getCurrentTimeWithLanguage(getActivity()).replace(":", "") + ".jpg";
                    child_position = childPosition;
                    group_position = groupPosition;
                    path = str + _pathforcheck;

                    startCameraActivity(1);
                }
            });
*/


            if(childData.getPresent()==1){
                holder.radio_yes.setBackground(getActivity().getResources().getDrawable(R.mipmap.yes_green));
                holder.radio_no.setBackground(getActivity().getResources().getDrawable(R.mipmap.no_white));
                //holder.img_cam.setVisibility(View.VISIBLE);
                holder.radio_yes.setChecked(true);
                //notifyDataSetChanged();
            }
            else if(childData.getPresent()==0){
                holder.radio_yes.setBackground(getActivity().getResources().getDrawable(R.mipmap.yes_white));
                holder.radio_no.setBackground(getActivity().getResources().getDrawable(R.mipmap.no_red));
                //holder.img_cam.setVisibility(View.GONE);
                holder.radio_no.setChecked(true);
                //notifyDataSetChanged();
            }else {
                holder.radio_yes.setBackground(getActivity().getResources().getDrawable(R.mipmap.yes_white));
                holder.radio_no.setBackground(getActivity().getResources().getDrawable(R.mipmap.no_white));
                //holder.img_cam.setVisibility(View.GONE);
                holder.rg.clearCheck();
                //notifyDataSetChanged();
            }

           /* if(childData.getImgPath().equals("")){
                holder.img_cam.setBackgroundResource(R.mipmap.camera_orange);
            }
            else {
                holder.img_cam.setBackgroundResource(R.mipmap.camera_green);
            }*/

            //empty check color change
            if (!checkflag) {
                boolean tempflag = false;

                if (tempflag) {
                    //holder.cardView.setCardBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
                    holder.lin_category.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
                } else {
                    holder.lin_category.setBackgroundColor(getResources().getColor(R.color.white));
                }

            }

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }

    public class ViewHolder {
        CardView cardView;
        TextView txt_skuName;
        LinearLayout lin_category;
        RadioGroup rg;
        RadioButton radio_yes;
        RadioButton radio_no;
        ImageView img_cam;
    }

    private void startCameraActivity(int pos) {
        try {

            Log.i("Stock & Facing ", "startCameraActivity()");
            File file = new File(path);
            outputFileUri = FileProvider.getUriForFile(getActivity(), "cpm.com.gskmtorange.fileprovider", file);

            String defaultCameraPackage = "";
            final PackageManager packageManager = getActivity().getPackageManager();
            List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
            for (int n = 0; n < list.size(); n++) {
                if ((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                  /*  Log.e("TAG", "Installed Applications  : " + list.get(n).loadLabel(packageManager).toString());
                    Log.e("TAG", "package name  : " + list.get(n).packageName);*/

                    //temp value in case camera is gallery app above jellybean
                    String packag = list.get(n).loadLabel(packageManager).toString();
                    if (packag.equalsIgnoreCase("Gallery") || packag.equalsIgnoreCase("Galeri") || packag.equalsIgnoreCase("الاستوديو")) {
                        gallery_package = list.get(n).packageName;
                    }

                    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        if (packag.equalsIgnoreCase("Camera") || packag.equalsIgnoreCase("Kamera") || packag.equalsIgnoreCase("الكاميرا")) {
                            defaultCameraPackage = list.get(n).packageName;
                            break;
                        }
                    } else {

                        if (packag.equalsIgnoreCase("Camera") || packag.equalsIgnoreCase("Kamera") || packag.equalsIgnoreCase("الكاميرا")) {

                            defaultCameraPackage = list.get(n).packageName;
                            break;
                        }
                    }
                }
            }

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            intent.setPackage(defaultCameraPackage);
            //startActivityForResult(intent, 1);
            startActivityForResult(intent, pos);
            //startActivityForResult(intent, position);

        } catch (ActivityNotFoundException e) {
            e.printStackTrace();

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            intent.setPackage(gallery_package);
            //startActivityForResult(intent, 0);
            startActivityForResult(intent, pos);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("Stock & Facing", "resultCode: " + resultCode + " requestCode: " + requestCode);
        *//*switch (resultCode) {
            case 0:
                Log.e("Stock & Facing", "User cancelled");

                break;
            case -1:
                if (_pathforcheck != null && !_pathforcheck.equals("")) {
                    if (new File(str + _pathforcheck).exists()) {
                        img = _pathforcheck;
                        t2PAdapter.notifyDataSetChanged();
                        _pathforcheck = "";
                    }
                }
                break;
        }*//*

        switch (requestCode) {
            case 1:
                if (resultCode == 0) {
                    Log.e("Stock & Facing", "User cancelled");
                } else if (resultCode == -1) {
                    if (_pathforcheck != null && !_pathforcheck.equals("")) {
                        if (new File(str + _pathforcheck).exists()) {

                            hashMapListChildData.get(headerDataList.get(group_position)).get(child_position).setImgPath(_pathforcheck);

                            expandableListView.invalidateViews();
                            adapter.notifyDataSetChanged();
                            _pathforcheck = "";
                        }
                    }
                }
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }*/

    boolean validateData(List<MSL_AvailabilityStockFacingGetterSetter> listDataHeader,
                         HashMap<MSL_AvailabilityStockFacingGetterSetter, List<PharmaMSLInsertedGetterSetter>> listDataChild) {
        boolean flag = true, child_flag = true, all_flag = true;

        checkHeaderArray.clear();
        int childCount = 0;

        //temp list data for empty only
        List<MSL_AvailabilityStockFacingGetterSetter> listDataHeaderTemp = new ArrayList<>();

        HashMap<MSL_AvailabilityStockFacingGetterSetter, List<MSL_AvailabilityStockFacingGetterSetter>> listDataChildTemp = new HashMap<>();


        for (int i = 0; i < listDataHeader.size(); i++) {
            flag = true;

            List<MSL_AvailabilityStockFacingGetterSetter> listTemp = new ArrayList<>();

            for (int j = 0; j < listDataChild.get(listDataHeader.get(i)).size(); j++) {

                child_flag = true;

                PharmaMSLInsertedGetterSetter data = listDataChild.get(listDataHeader.get(i)).get(j);

                int isChecked = data.getPresent();
                //String imgPath = data.getImgPath();

                if(isChecked==-1){

                    scrollPosition = i + j + childCount;

                    flag = false;
                    Error_Message = getResources().getString(R.string.msg_select_present);
                    break;
                }
               /* else if (isChecked==1) {

                    if(imgPath == null || imgPath.equals("")){

                        scrollPosition = i + j + childCount;

                        flag = false;
                        Error_Message = getResources().getString(R.string.click_image);
                        break;
                    }
                }*/
                /*if (!child_flag)
                    listTemp.add(data);*/

            }

            if (flag == false) {
                //checkflag = false;
               /* listDataHeaderTemp.add(listDataHeader.get(i));
                listDataChildTemp.put(listDataHeader.get(i), listTemp);*/

                if (!checkHeaderArray.contains(i)) {
                    checkHeaderArray.add(i);
                }
                checkflag = false;
                break;
            } else {
                childCount = childCount + listDataChild.get(listDataHeader.get(i)).size();
                checkflag = true;
            }
        }

       /* if (!all_flag) {
            adapter = new ExpandableListAdapter(this, listDataHeaderTemp, listDataChildTemp);
            expandableListView.setAdapter(adapter);
            checkflag = false;
        } else {
            checkflag = true;
        }*/

        return checkflag;
    }
}
