package com.tofly.ghrq_route.common.ui.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.planPointRepair.domain.AdjustRepairContent;
import com.tofly.ghrq_route.module.planPointRepair.domain.MaintainAdjustParam;
import com.tofly.ghrq_route.module.planPointRepair.domain.MaintainSelectParam;

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

public class CheckBoxListView {
    private Context mContext;
    private List<MaintainSelectParam> optionList;
    private AdjustRepairContent mRepairContent;
    private List<String> mSelectedList = new ArrayList<>();
    protected OnItemSelectListener mItemSelectListener;
    private List<CheckedTextView> checkViews = new ArrayList<>();

    private int COLUMN = 2;
    /**
     * Context mContext:上下文； String title: 标题； String[] str:内部TextView显示的字符串的数组
     * int inid:每个TextView的id
     */

    public interface OnItemSelectListener {
        void onItemSelect(String id, boolean selected);
    }

    public void setOnItemSelectListener(OnItemSelectListener listener) {
        this.mItemSelectListener = listener;
    }

    public CheckBoxListView(Context context, List<MaintainSelectParam> options) {
        mContext = context;
        optionList = options;
    }

    public CheckBoxListView(Context context, List<MaintainSelectParam> options, AdjustRepairContent repairContent) {
        mContext = context;
        optionList = options;
        mRepairContent = repairContent;
    }

    public void setCOLUMN(int COLUMN) {
        this.COLUMN = COLUMN;
    }

    public View getViewWithNoTitle() {
        LinearLayout rootLayout = new LinearLayout(mContext);
        rootLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        rootLayout.setOrientation(LinearLayout.VERTICAL);

        int itemHeight = BaseApp.dip2px(32);

        int flag = optionList.size() % COLUMN;
        if (flag != 0) {
            flag = 1;
        } else {
            flag = 0;
        }
        int outside = optionList.size() / COLUMN + flag;
        for (int i = 0; i < outside; i++) {
            final LinearLayout mLinearLayout = new LinearLayout(mContext);
            mLinearLayout.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, itemHeight));
            mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

            int show = i * COLUMN;
            for (int j = 0; j < COLUMN; j++) {
                final View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_checkbox, mLinearLayout, false);
                CheckedTextView checkedView = rootView.findViewById(R.id.checked_text);
                if (show + j < optionList.size()){
                    MaintainSelectParam selectParam = optionList.get(show + j);
                    String title = selectParam.getName();
                    if (title != null){
                        checkedView.setText(title);
                    }
                    checkedView.setChecked(selectParam.isSelected());
                    checkedView.setTag(selectParam);
                    checkedView.setOnClickListener(view -> {
                        checkedView.toggle();
                        boolean selected = checkedView.isChecked();
                        MaintainSelectParam param = (MaintainSelectParam) checkedView.getTag();
                        if (mItemSelectListener != null) {
                            String selectId = "";
                            if (param != null)
                                selectId = param.getId();
                            mItemSelectListener.onItemSelect(selectId, selected);
                        }
                    });
                }else {
                    rootView.setVisibility(View.INVISIBLE);
                }
                mLinearLayout.addView(rootView);
            }
            rootLayout.addView(mLinearLayout);
        }
        return rootLayout;
    }

    public View getViewWithParentTitle(MaintainAdjustParam parentParam) {
        LinearLayout rootLayout = new LinearLayout(mContext);
        rootLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        rootLayout.setTag(mRepairContent);

        final View titleView = LayoutInflater.from(mContext).inflate(R.layout.item_checkbox_parent, rootLayout, false);
        TextView textTitle = titleView.findViewById(R.id.text_title);
        textTitle.setText(String.format("%s:", parentParam.getName()));

        CheckedTextView checkedRoot = titleView.findViewById(R.id.checked_text);
        boolean isNone = mRepairContent.getExist() == 0;
        checkedRoot.setChecked(isNone);
        checkedRoot.setOnClickListener(view -> {
            checkedRoot.toggle();
            boolean selected = checkedRoot.isChecked();
            mRepairContent.setExist(selected ? 0 : 1);
            if (selected) {
                if (checkViews != null && checkViews.size() > 0) {
                    for (CheckedTextView checkView : checkViews) {
                        checkView.setChecked(false);
                    }
                }
                setSelectNone();
            }else {
                List<String> content = mRepairContent.getMenuIds();
                if (content != null)
                    content.clear();
            }
        });
        checkedRoot.setVisibility(mRepairContent.isHasNone() ? View.VISIBLE : View.GONE);
        rootLayout.addView(titleView);

        int itemHeight = BaseApp.dip2px(32);
        int flag = optionList.size() % COLUMN;
        if (flag != 0) {
            flag = 1;
        } else {
            flag = 0;
        }
        int outside = optionList.size() / COLUMN + flag;
        for (int i = 0; i < outside; i++) {
            final LinearLayout mLinearLayout = new LinearLayout(mContext);
            mLinearLayout.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, itemHeight));
            mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

            int show = i * COLUMN;
            for (int j = 0; j < COLUMN; j++) {
                final View rootView = LayoutInflater.from(mContext).inflate(R.layout.item_checkbox, mLinearLayout, false);
                CheckedTextView checkedView = rootView.findViewById(R.id.checked_text);
                if (show + j < optionList.size()){
                    MaintainSelectParam selectParam = optionList.get(show + j);
                    if (selectParam.getName().equals(mContext.getString(R.string.none))) {
                        continue;
                    }
                    String title = selectParam.getName();
                    if (title != null){
                        checkedView.setText(title);
                    }
                    boolean isSelected = selectParam.isSelected();
                    checkedView.setChecked(isSelected);
                    setSelectedItem(selectParam.getId(), isSelected);
                    checkedView.setTag(selectParam);
                    checkedView.setOnClickListener(view -> {
                        checkedView.toggle();
                        boolean selected = checkedView.isChecked();
                        MaintainSelectParam param = (MaintainSelectParam)checkedView.getTag();
                        String selectId = "";
                        if (param != null)
                            selectId = param.getId();
                        setSelectedItem(selectId, selected);
                        if (mSelectedList.size() > 0) {
                            mRepairContent.setExist(1);
                            checkedRoot.setChecked(false);
                        }
                    });
                    checkViews.add(checkedView);
                }else {
                    rootView.setVisibility(View.INVISIBLE);
                }
                mLinearLayout.addView(rootView);
            }
            rootLayout.addView(mLinearLayout);
        }

        if (isNone) {
            setSelectNone();
        }

        final View noteView = LayoutInflater.from(mContext).inflate(R.layout.item_checkbox_note, rootLayout, false);
        EditText editNote = noteView.findViewById(R.id.edit_note);
        if (mRepairContent.getNote() != null) {
            editNote.setText(mRepairContent.getNote());
        }
        editNote.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (mRepairContent != null)
                    mRepairContent.setNote(editable.toString());
            }
        });
        rootLayout.addView(noteView);
        return rootLayout;
    }

    private void setSelectNone() {
        List<String> content = new ArrayList<>();
        MaintainSelectParam noneParam = mRepairContent.getNoneParam();
        if (mRepairContent.isHasNone() && noneParam != null) {
            content.add(noneParam.getId());
        }
        mRepairContent.setMenuIds(content);
        mSelectedList.clear();
    }

    private void setSelectedItem(String id, boolean selected) {
        if (id != null && !id.isEmpty()) {
            if (selected) {
                if (!mSelectedList.contains(id)) {
                    mSelectedList.add(id);
                }
            }else {
                mSelectedList.remove(id);
            }

            if (mSelectedList.size() > 0) {
                mRepairContent.setMenuIds(mSelectedList);
            }else {
                List<String> content = mRepairContent.getMenuIds();
                if (content != null)
                    content.clear();
            }
        }
    }
}
