package kr.co.icontrols.iotplatform;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

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

public class FaqActivity extends AppCompatActivity implements View.OnClickListener {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_faq);

        // get the listview
        expListView = findViewById(R.id.listView);
        expListView.setGroupIndicator(null);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
    }

    @Override
    public void onClick(View v) {
        /*
        final View fv = v;
        fv.setEnabled(false);
        Timer buttonTimer = new Timer();
        buttonTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fv.setEnabled(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {

                        }
                    }
                });
            }
        }, 500);
        */

        switch (v.getId()) {
            case R.id.btnBack:
                onBackPressed();
                break;
        }
    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context _context;
        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<String, List<String>> _listDataChild;

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

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

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

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

            final String childText = (String) getChild(groupPosition, childPosition);

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

            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);

            txtListChild.setText(childText);
            return convertView;
        }

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

        @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(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandlistview_list_group, null);
            }

            TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
            lblListHeader.setText(headerTitle);
            ImageView imgArrow = (ImageView) convertView.findViewById(R.id.imgArrow);
            if(isExpanded){
                //lblListHeader.setTextColor(Color.rgb(0xfb,0x8a,0x3c));
                imgArrow.setImageResource(R.drawable.list_sel_arrow_open);
            } else {
                //lblListHeader.setTextColor(Color.rgb(0x00,0x00,0x00));
                imgArrow.setImageResource(R.drawable.list_sel_arrow);
            }

            return convertView;
        }

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

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

    /**
     * 자주하는 질문 리스트 구성 함수
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add(getString(R.string.faq_title_1));
        listDataHeader.add(getString(R.string.faq_title_2));
        listDataHeader.add(getString(R.string.faq_title_3));
        listDataHeader.add(getString(R.string.faq_title_4));
        listDataHeader.add(getString(R.string.faq_title_5));
        listDataHeader.add(getString(R.string.faq_title_6));
        listDataHeader.add(getString(R.string.faq_title_7));
        listDataHeader.add(getString(R.string.faq_title_8));
        listDataHeader.add(getString(R.string.faq_title_9));
        listDataHeader.add(getString(R.string.faq_title_10));
        listDataHeader.add(getString(R.string.faq_title_11));
        listDataHeader.add(getString(R.string.faq_title_12));
        listDataHeader.add(getString(R.string.faq_title_13));
        listDataHeader.add(getString(R.string.faq_title_14));
        listDataHeader.add(getString(R.string.faq_title_15));
        listDataHeader.add(getString(R.string.faq_title_16));
        listDataHeader.add(getString(R.string.faq_title_17));
        listDataHeader.add(getString(R.string.faq_title_18));

        // Adding child data
        List<String> faq1 = new ArrayList<String>();
        faq1.add(getString(R.string.faq_description_1));

        List<String> faq2 = new ArrayList<String>();
        faq2.add(getString(R.string.faq_description_2));

        List<String> faq3 = new ArrayList<String>();
        faq3.add(getString(R.string.faq_description_3));

        List<String> faq4 = new ArrayList<String>();
        faq4.add(getString(R.string.faq_description_4));

        List<String> faq5 = new ArrayList<String>();
        faq5.add(getString(R.string.faq_description_5));

        List<String> faq6 = new ArrayList<String>();
        faq6.add(getString(R.string.faq_description_6));

        List<String> faq7 = new ArrayList<String>();
        faq7.add(getString(R.string.faq_description_7));

        List<String> faq8 = new ArrayList<String>();
        faq8.add(getString(R.string.faq_description_8));

        List<String> faq9 = new ArrayList<String>();
        faq9.add(getString(R.string.faq_description_9));

        List<String> faq10 = new ArrayList<String>();
        faq10.add(getString(R.string.faq_description_10));

        List<String> faq11 = new ArrayList<String>();
        faq11.add(getString(R.string.faq_description_11));

        List<String> faq12 = new ArrayList<String>();
        faq12.add(getString(R.string.faq_description_12));

        List<String> faq13 = new ArrayList<String>();
        faq13.add(getString(R.string.faq_description_13));

        List<String> faq14 = new ArrayList<String>();
        faq14.add(getString(R.string.faq_description_14));

        List<String> faq15 = new ArrayList<String>();
        faq15.add(getString(R.string.faq_description_15));

        List<String> faq16 = new ArrayList<String>();
        faq16.add(getString(R.string.faq_description_16));

        List<String> faq17 = new ArrayList<String>();
        faq17.add(getString(R.string.faq_description_17));

        List<String> faq18 = new ArrayList<String>();
        faq18.add(getString(R.string.faq_description_18));

        listDataChild.put(listDataHeader.get(0), faq1); // Header, Child data
        listDataChild.put(listDataHeader.get(1), faq2);
        listDataChild.put(listDataHeader.get(2), faq3);
        listDataChild.put(listDataHeader.get(3), faq4);
        listDataChild.put(listDataHeader.get(4), faq5);
        listDataChild.put(listDataHeader.get(5), faq6);
        listDataChild.put(listDataHeader.get(6), faq7);
        listDataChild.put(listDataHeader.get(7), faq8);
        listDataChild.put(listDataHeader.get(8), faq9);
        listDataChild.put(listDataHeader.get(9), faq10);
        listDataChild.put(listDataHeader.get(10), faq11);
        listDataChild.put(listDataHeader.get(11), faq12);
        listDataChild.put(listDataHeader.get(12), faq13);
        listDataChild.put(listDataHeader.get(13), faq14);
        listDataChild.put(listDataHeader.get(14), faq15);
        listDataChild.put(listDataHeader.get(15), faq16);
        listDataChild.put(listDataHeader.get(16), faq17);
        listDataChild.put(listDataHeader.get(17), faq18);
    }
}