package com.navinfo.outdoor.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.bean.BolBean;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CapacityMeasureAdapter extends RecyclerView.Adapter<CapacityMeasureAdapter.ViewHolder> {
    private List<BolBean> measureList = new ArrayList<>();
    private Context context;
    private Map<String, String> checkedMap;

    public Map<String, String> getCheckedMap() {
        return checkedMap;
    }

    public CapacityMeasureAdapter(Context context) {
        this.context = context;
        this.checkedMap = new LinkedHashMap<>();
    }

    public void setMeasureList(List<BolBean> measureList) {
        this.measureList.addAll(measureList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         View inflate = LayoutInflater.from(context).inflate(R.layout.measure_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final boolean single = measureList.get(position).isSingle();//是否多选
       // holder.tvMeasure.setText((position + 1)+"、" + measureList.get(position).getQuestion());
        if (single) {
            holder.tvMeasure.setText((position + 1)+"、" + measureList.get(position).getQuestion()+"(单选)");
            RadioGroup radioGroup = new RadioGroup(context);
            radioGroup.setOrientation(LinearLayout.VERTICAL);
            holder.llOption.addView(radioGroup);
            List<?> optionList = measureList.get(position).getOption();
            if (optionList != null && !optionList.isEmpty()) {
                for (int i = 0; i < optionList.size(); i++) {
                    obtainOptionLayout(radioGroup, i, (String) optionList.get(i), true,measureList.get(position).getId());
                }
            }
        } else {
            holder.tvMeasure.setText((position + 1)+"、" + measureList.get(position).getQuestion()+"(多选)");
            List<?> optionList = measureList.get(position).getOption();
            if (optionList != null && !optionList.isEmpty()) {
                for (int i = 0; i < optionList.size(); i++) {
                    obtainOptionLayout(holder.llOption, i, (String) optionList.get(i), false, measureList.get(position).getId());
                }
            }
        }
    }

    private void obtainOptionLayout(ViewGroup parentLayout, int optionIndex, String option, boolean isSingle, String  questionId) {
        LinearLayout optionLayer = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.capacity_measure_option, null);
        parentLayout.addView(optionLayer);
        RadioButton tvOptionIndex = optionLayer.findViewById(R.id.tv_option_layer_index);
        CheckBox cbOptionIndex = optionLayer.findViewById(R.id.cb_option_layer_index);
        if (isSingle) {//单选
            tvOptionIndex.setVisibility(View.VISIBLE);
            cbOptionIndex.setVisibility(View.GONE);
            tvOptionIndex.setText(getOptionIndex(optionIndex));
        } else {
            tvOptionIndex.setVisibility(View.GONE);
            cbOptionIndex.setVisibility(View.VISIBLE);
            cbOptionIndex.setText(getOptionIndex(optionIndex));
        }
        tvOptionIndex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkedChangeRadioButton(parentLayout, buttonView);

                   // Toast.makeText(context, "option" + option, Toast.LENGTH_SHORT).show();

                    updateCheckedMap(questionId, buttonView, parentLayout);
                }

            }
        });
        cbOptionIndex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateCheckedMap(questionId, buttonView, parentLayout);
            }
        });

      //  ImageView ivOption = optionLayer.findViewById(R.id.iv_option_img);

        TextView tvOptionContent = optionLayer.findViewById(R.id.tv_option_content);

        if (option != null) {
            if (option.startsWith("http")) {
              //  Glide.with(context).load(option).into(ivOption);
            } else {
                tvOptionContent.setText(option);
            }
        }
    }

    private void checkedChangeRadioButton(ViewGroup group, CompoundButton currentButton) {
        if (group != null && group.getChildCount() > 0) {
            for (int i = 0; i < group.getChildCount(); i++) {
                if (group.getChildAt(i) instanceof ViewGroup) {
                    checkedChangeRadioButton((ViewGroup) group.getChildAt(i), currentButton);
                } else if (group.getChildAt(i) instanceof CompoundButton && group.getChildAt(i).isShown() && group.getChildAt(i) != currentButton) {
                    ((CompoundButton) group.getChildAt(i)).setChecked(false);
                }
            }
        }
    }

    //indext内容
    private String getOptionIndex(int index) {
        char A = (char) (65 + index);
        return  A + "：";


    }

    @Override
    public int getItemCount() {
        return measureList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvMeasure;
        LinearLayout llOption;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            llOption = itemView.findViewById(R.id.ll_option);
            tvMeasure = itemView.findViewById(R.id.tv_measure_text1);

        }
    }

    /**
     * 获取被勾选的数据
     */
    private void updateCheckedMap(String questionId, CompoundButton currentButton, ViewGroup group) {
        StringBuilder resultStr = null;
        if (group != null) {
            resultStr = new StringBuilder("");
            List<CompoundButton> resultList = new ArrayList<>();
            getCompoundButton(group, resultList);
            if (resultList != null && !resultList.isEmpty()) {
                for (int i = 0; i < resultList.size(); i++) {
                    if (((CompoundButton) resultList.get(i)).isChecked()) {
                        resultStr.append(i).append(",");
                    }
                }
            }
        }
        if (resultStr != null) {
            String result = "";
            if (resultStr.toString().length() > 0) {
                result = resultStr.substring(0, resultStr.length() - 1);
            }
            checkedMap.put(questionId, result);
            Log.d("checked", result);
        }
    }

    /**
     * 获取该viewGroup的所有子CompoundButton
     */
    private void getCompoundButton(ViewGroup group, List<CompoundButton> compoundButtonList) {
        if (group != null) {
            for (int i = 0; i < group.getChildCount(); i++) {
                if (group.getChildAt(i) instanceof ViewGroup) {
                    getCompoundButton((ViewGroup) group.getChildAt(i), compoundButtonList);
                } else if (group.getChildAt(i) instanceof CompoundButton && group.getChildAt(i).isShown()) {
                    compoundButtonList.add((CompoundButton) group.getChildAt(i));
                }
            }
        }
    }
}
