package in.techware.carrefour.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import in.techware.carrefour.R;
import in.techware.carrefour.adapters.SelectRecyclerAdapter;
import in.techware.carrefour.config.Config;
import in.techware.carrefour.config.TypefaceCache;
import in.techware.carrefour.model.BasicDataBean;
import in.techware.carrefour.model.DepartmentBean;
import in.techware.carrefour.model.OriginBean;
import in.techware.carrefour.model.SectionBean;
import in.techware.carrefour.util.AppConstants;
import in.techware.carrefour.util.FileOp;

/**
 * Created by Jemsheer K D on 05 December, 2016.
 * Package in.techware.carrefour.dialogs
 * Project Carrefour
 */

public class SelectDialog {

    private final Activity mContext;
    private List<OriginBean> originTypeList;
    private ArrayList<String> stringList;
    private String subID;
    private String ID;
    private Typeface typeface;
    private String action;
    private SelectDialogActionListener selectDialogActionListener;
    private Dialog dialogSelect;
    private ListView lvSelect;
    private RecyclerView rvSelect;
    private String[] choiceList;
    //    private SelectAdapter adapter;
    private SelectRecyclerAdapter adapter;
    private TextView txtTitle;
    private BasicDataBean basicDataBean;

    public SelectDialog(Activity mContext, String action) {
        this.mContext = mContext;
        this.action = action;

        init(mContext);
    }

    public SelectDialog(Activity mContext, String action, String ID) {
        this.mContext = mContext;
        this.action = action;
        this.ID = ID;

        init(mContext);
    }

    public SelectDialog(Activity mContext, String action, String ID, String subID) {
        this.mContext = mContext;
        this.action = action;
        this.ID = ID;
        this.subID = subID;

        init(mContext);
    }

    public SelectDialog(Activity mContext, String action, ArrayList<String> stringList) {
        this.mContext = mContext;
        this.action = action;
        this.stringList = stringList;

        init(mContext);
    }

    public SelectDialog(Activity mContext, String action, List<OriginBean> originTypeList) {
        this.mContext = mContext;
        this.action = action;
        this.originTypeList = originTypeList;

        init(mContext);
    }

    private void init(Context mContext) {
        try {
            typeface = TypefaceCache.getInstance().getTypeface(mContext.getApplicationContext(), "Roboto-Regular.ttf");
        } catch (Exception e) {
            e.printStackTrace();
        }
//        mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);

        setSelectDialog();

        if (Config.getInstance().getBasicDataBean() != null) {
            basicDataBean = Config.getInstance().getBasicDataBean();
        } else {
            basicDataBean = new FileOp(mContext.getApplicationContext()).readBasicDataBean();
        }
    }


    public void setTitle(String title) {
        dialogSelect.setTitle(title);
        txtTitle.setText(title);
    }

    public void show() {
        populate();
        dialogSelect.show();
    }

    private void setSelectDialog() {

        dialogSelect = new Dialog(mContext, R.style.ThemeDialogCustom_NoBackground);
        dialogSelect.setContentView(R.layout.dialog_select);
        dialogSelect.setTitle(R.string.title_select);

//        lvSelect = (ListView) dialogSelect.findViewById(R.id.lv_dialog_select);
        txtTitle = (TextView) dialogSelect.findViewById(R.id.txt_dialog_select_title);

        rvSelect = dialogSelect.findViewById(R.id.rv_dialog_select);
        rvSelect.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvSelect.setLayoutManager(linearLayoutManager);

    }

    private void populate() {

        /*if (AppConstants.ACTION_CHOOSE_PREFIX.equals(action)) {
            choiceList = mContext.getResources().getStringArray(R.array.prefix);

            System.out.println(Arrays.toString(choiceList));
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        } else if (AppConstants.ACTION_CHOOSE_SUFFIX.equals(action)) {
            choiceList = mContext.getResources().getStringArray(R.array.suffix);
            System.out.println(Arrays.toString(choiceList));
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        }else if (AppConstants.ACTION_CHOOSE_CAUSE_OF_DEATH.equals(action)) {
            choiceList = mContext.getResources().getStringArray(R.array.cause_of_death);
            System.out.println(Arrays.toString(choiceList));
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        } else*/
        if (AppConstants.ACTION_CHOOSE_AGE.equals(action)) {
            choiceList = new String[75 - 18];
            int num = 75 - 18;
            for (int i = 0; i < num; i++) {
                choiceList[i] = "" + (i + 18);
            }
            populateAdapter();
        }/*else if (AppConstants.ACTION_CHOOSE_RELIGION.equals(action)) {
            choiceList = new String[basicDataBean.getReligions().size()];
            for (int i = 0; i < basicDataBean.getReligions().size(); i++) {
                choiceList[i] = basicDataBean.getReligions().get(i).getName();
            }
            System.out.println(Arrays.toString(choiceList));
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        } else if (AppConstants.ACTION_CHOOSE_DAY.equals(action)) {
            choiceList = mContext.getResources().getStringArray(R.array.day);
            System.out.println(Arrays.toString(choiceList));
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        } else if (AppConstants.ACTION_CHOOSE_MONTH.equals(action)) {
            choiceList = mContext.getResources().getStringArray(R.array.month);
            System.out.println(Arrays.toString(choiceList));
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        } else if (AppConstants.ACTION_CHOOSE_YEAR.equals(action)) {
            choiceList = new String[Calendar.getInstance().get(Calendar.YEAR) - AppConstants.YEAR_START - 18 + 1];
            int num = Calendar.getInstance().get(Calendar.YEAR) - AppConstants.YEAR_START - 18;

            for (int i = 0, j = num; i <= num; i++, j--) {
                choiceList[j] = "" + (i + AppConstants.YEAR_START);
            }
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        }*//*else if (AppConstants.ACTION_CHOOSE_QUANTITY.equals(action)) {
            UserInterestBean userInterestBean = (UserInterestBean) getIntent().getSerializableExtra("category");
            interestList = userInterestBean.getInterest();
            choiceList = new String[interestList.size()];
            for (int i = 0; i < interestList.size(); i++) {
                choiceList[i] = interestList.get(i).getHeight();
            }
            lvSelect.setAdapter(new SelectAdapter());
        }*/ else if (AppConstants.ACTION_CHOOSE_COUNTRY.equals(action)) {
            choiceList = new String[basicDataBean.getCountries().size()];
            for (int i = 0; i < basicDataBean.getCountries().size(); i++) {
                choiceList[i] = basicDataBean.getCountries().get(i).getName();
            }
            System.out.println(Arrays.toString(choiceList));
            populateAdapter();
        } else if (AppConstants.ACTION_CHOOSE_ORIGIN.equals(action)) {
            choiceList = new String[originTypeList.size()];
            for (int i = 0; i < originTypeList.size(); i++) {
                choiceList[i] = originTypeList.get(i).getOriginName();
            }
            System.out.println(Arrays.toString(choiceList));
            populateAdapter();
        }else if (AppConstants.ACTION_CHOOSE_DEPARTMENT.equals(action)) {
            choiceList = new String[basicDataBean.getDepartmentList().size()];
            for (int i = 0; i < basicDataBean.getDepartmentList().size(); i++) {
                choiceList[i] = basicDataBean.getDepartmentList().get(i).getDepartmentName();
            }
            System.out.println(Arrays.toString(choiceList));
            populateAdapter();
        }else if (AppConstants.ACTION_CHOOSE_SECTION.equals(action)) {
            choiceList = new String[basicDataBean.getSectionList().size()];
            for (int i = 0; i < basicDataBean.getSectionList().size(); i++) {
                choiceList[i] = basicDataBean.getSectionList().get(i).getSectionName();
            }
            System.out.println(Arrays.toString(choiceList));
            populateAdapter();
        }else if (AppConstants.ACTION_CHOOSE_PRINTER.equals(action)) {
            choiceList = new String[basicDataBean.getPrinterList().size()];
            for (int i = 0; i < basicDataBean.getPrinterList().size(); i++) {
                choiceList[i] = basicDataBean.getPrinterList().get(i).getName();
            }
            System.out.println(Arrays.toString(choiceList));
            populateAdapter();
        } else if (AppConstants.ACTION_CHOOSE_GONDOLA.equals(action)) {
            choiceList = new String[stringList.size()];
            for (int i = 0; i < stringList.size(); i++) {
                choiceList[i] = stringList.get(i);
            }
            System.out.println(Arrays.toString(choiceList));
            populateAdapter();
        } /*else if (AppConstants.ACTION_CHOOSE_STATE.equals(action)) {
            if (basicDataBean.getCountry(ID).getStates() != null) {
                choiceList = new String[basicDataBean.getCountry(ID).getStates().size()];
                for (int i = 0; i < basicDataBean.getCountry(ID).getStates().size(); i++) {
                    choiceList[i] = basicDataBean.getCountry(ID).getStates().get(i).getName();
                }
                System.out.println(Arrays.toString(choiceList));
            }
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        } else if (AppConstants.ACTION_CHOOSE_DISTRICT.equals(action)) {
            if (basicDataBean.getState(ID, subID).getDistricts() != null) {
                choiceList = new String[basicDataBean.getState(ID, subID).getDistricts().size()];
                for (int i = 0; i < basicDataBean.getState(ID, subID).getDistricts().size(); i++) {
                    choiceList[i] = basicDataBean.getState(ID, subID).getDistricts().get(i).getName();
                }
                System.out.println(Arrays.toString(choiceList));
            }
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        }  else if (AppConstants.ACTION_CHOOSE_STATE.equals(action)) {
            if (basicDataBean.getStateList(ID) != null) {
                choiceList = new String[basicDataBean.getStateList(ID).size()];
                for (int i = 0; i < basicDataBean.getStateList(ID).size(); i++) {
                    choiceList[i] = basicDataBean.getStateList(ID).get(i).getName();
                }
                System.out.println(Arrays.toString(choiceList));
            }
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        } else if (AppConstants.ACTION_CHOOSE_DISTRICT.equals(action)) {
            if (basicDataBean.getDistrictList(ID) != null) {
                choiceList = new String[basicDataBean.getDistrictList(ID).size()];
                for (int i = 0; i < basicDataBean.getDistrictList(ID).size(); i++) {
                    choiceList[i] = basicDataBean.getDistrictList(ID).get(i).getName();
                }
                System.out.println(Arrays.toString(choiceList));
            }
            if (adapter == null)
                adapter = new SelectAdapter();
            else
                adapter.notifyDataSetChanged();
            lvSelect.setAdapter(adapter);
        }*/

    }

    private void populateAdapter() {
        if (adapter == null) {
//                adapter = new SelectAdapter();
            adapter = new SelectRecyclerAdapter(mContext, choiceList, action);
            adapter.setSelectRecyclerAdapterListener(
                    new SelectRecyclerAdapter.SelectRecyclerAdapterListener() {
                        @Override
                        public void onItemSelected(int position, String value) {
                            selectDialogActionListener.actionCompletedSuccessfully(position, value);
                            dialogSelect.dismiss();
                        }
                    });
            rvSelect.setAdapter(adapter);
        } else {
            adapter.setAction(action);
            adapter.setChoiceList(choiceList);
            adapter.notifyDataSetChanged();
//            lvSelect.setAdapter(adapter);
        }
    }


 /*   private static class ViewHolder {
        private TextView txtName;
    }
*/

    public interface SelectDialogActionListener {
        void actionCompletedSuccessfully(int position, String value);

        void actionFailed(String errorMsg);
    }

    public SelectDialogActionListener getSelectDialogActionListener() {
        return selectDialogActionListener;
    }


    public void setSelectDialogActionListener(SelectDialogActionListener selectDialogActionListener) {
        this.selectDialogActionListener = selectDialogActionListener;
    }
}
