package com.unis.sxzcl.ui.grain_check;

import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseProviderMultiAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.model.bean.detail.DetailItemBean;
import com.unis.model.bean.detail.DetailItemType;
import com.unis.sxzcl.R;
import com.unis.sxzcl.databinding.TemplateTvBinding;
import com.unis.sxzcl.widget.dialog.InputDialog;
import com.xyxsbj.mylibrary.tool.DateTool;

import org.jetbrains.annotations.NotNull;

import java.util.Date;
import java.util.List;

/**
 * 包名: com.unis.sxzcl.ui.grain_check
 * 类名: GrainCheckDetailAdapter
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/11 10:30
 * 更新人:
 * 更新时间: 2020/12/11 10:30
 * 更新说明:
 * 版本号: 1.0
 */
public class GrainCheckDetailAdapter extends BaseProviderMultiAdapter<DetailItemBean> implements LoadMoreModule {

    private String pageType = "add";

    public GrainCheckDetailAdapter() {
        super();
        //添加多布局
        addItemProvider(new RadioProvider());
        addItemProvider(new InputProvider());
    }

    @Override
    protected int getItemType(@NotNull List<? extends DetailItemBean> list, int i) {
        return list.get(i).getItemType();
    }

    public void setPageType(String pageType) {
        if (pageType == null) return;
        this.pageType = pageType;
    }

    /**
     * 单选提供者
     */
    public class RadioProvider extends BaseItemProvider<DetailItemBean> {

        private DetailItemBean currentItem;

        @Override
        public int getItemViewType() {
            return DetailItemType.radio;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_detail_radio;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, DetailItemBean item) {
            holder.getView(R.id.layout_container).setVisibility(item.isShow() ? View.VISIBLE : View.GONE);

            // 标题
            holder.setText(R.id.tv_title, item.getIndex() == null ? item.getTitle() : (item.getIndex() + "." + item.getTitle()));

            //时间
            holder.setText(R.id.tv_time, item.getTime() == null ? "" : item.getTime());

            // 选项创建
            LinearLayout layoutOption = holder.getView(R.id.layout_option);
            layoutOption.removeAllViews();
            for (int i = 0; i < item.getOption().length; i++) {
                layoutOption.addView(createdTextView(item, i));
            }

        }

        /**
         * 创建单选按钮
         */
        private TextView createdTextView(DetailItemBean item, int optionIndex) {
            // 初始化 单选按钮
            TextView textView = TemplateTvBinding.inflate(LayoutInflater.from(getContext())).getRoot();
            textView.setText(item.getOption()[optionIndex]);
            textView.setGravity(Gravity.CENTER_VERTICAL);
            // 选中项一致 改变图标
            if (item.getSelectIndex() != null && item.getSelectIndex().equals(optionIndex + "")) {
                Drawable dra = getContext().getResources().getDrawable(R.drawable.template_tv_left_sel_enable);
                textView.setCompoundDrawablesWithIntrinsicBounds(dra, null, null, null);
            } else {
                Drawable dra = getContext().getResources().getDrawable(R.drawable.template_tv_left_enable);
                textView.setCompoundDrawablesWithIntrinsicBounds(dra, null, null, null);
            }
            // 详情时禁用按钮和事件
            if (pageType.equals("detail")) {
                textView.setEnabled(false);
                return textView;
            }

            textView.setTag(item);
            textView.setOnClickListener(v -> {
                // 单选点击事件
                TextView tv = (TextView) v;
                currentItem = (DetailItemBean) v.getTag();

                // 得到所选值index
                String[] option = currentItem.getOption();
                String index = null;
                for (int i = 0; i < option.length; i++) {
                    if (tv.getText().toString().equals(option[i])) {
                        index = i + "";
                        break;
                    }
                }
                // 选中和历史一致不做任何操作
                if (currentItem.getSelectIndex() != null && currentItem.getSelectIndex().equals(index))
                    return;
                // 存储选中index
                currentItem.setSelectIndex(index);
                // 存储选中时间
                currentItem.setTime(DateTool.formatDate(new Date(), DateTool.YTDTD_DATE));

                // 刷新当前项
                notifyItemChanged(getItemPosition(currentItem));

                // 处理其他业务
                childShow();
                if (mCallback != null)
                    mCallback.onChangeListener(currentItem);
            });
            return textView;
        }

        // 子选项是否显示
        private void childShow() {
            String[] childIds = currentItem.getChildIds();
            if (childIds == null) return;
            boolean show = currentItem.getSelectIndex().equals("0");
            for (String cId : childIds) {
                for (DetailItemBean item : getData()) {
                    if (item.getId().equals(cId)) {
                        item.setShow(show);
                        notifyItemChanged(getItemPosition(item));
                        break;
                    }
                }
            }
        }
    }

    /**
     * 输入提供者
     */
    public class InputProvider extends BaseItemProvider<DetailItemBean> {

        private DetailItemBean currentItem;
        private InputDialog mInputDialog;

        @Override
        public int getItemViewType() {
            return DetailItemType.input;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_detail_input;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, DetailItemBean item) {
            holder.getView(R.id.layout_container).setVisibility(item.isShow() ? View.VISIBLE : View.GONE);
            holder.getView(R.id.tv_must).setVisibility(item.isMust() ? View.VISIBLE : View.GONE);

            // 标题
            holder.setText(R.id.tv_title, item.getIndex() == null ? item.getTitle() : (item.getIndex() + "." + item.getTitle()))
                    .setText(R.id.tv_input, item.getInputData());


            // 输入框点击事件
            TextView tvContent = holder.getView(R.id.tv_input);
            if (pageType.equals("detail")) {
                tvContent.setHint("");
                return;
            }
            tvContent.setOnClickListener(v -> {
                currentItem = item;
                showInputDialog();
            });
        }

        private void showInputDialog() {
            if (mInputDialog == null) {
                mInputDialog = new InputDialog(getContext()).setCallBack((dialog, data) -> {
                    dialog.dismiss();
                    currentItem.setInputData(data);
                    notifyItemChanged(getItemPosition(currentItem));
                    if (mCallback != null)
                        mCallback.onChangeListener(currentItem);
                });
            }
            mInputDialog.show();
            mInputDialog.getDialog().setTitle(currentItem.getTitle());
            mInputDialog.setContent(currentItem.getInputData());
        }
    }

    private ChangeCallback mCallback;

    public void setCallback(ChangeCallback callback) {
        mCallback = callback;
    }

    public interface ChangeCallback {
        void onChangeListener(DetailItemBean item);
    }

}
