package cc.winboll.studio.contacts.adapters;

/**
 * @Author ZhanGSKen<zhangsken@qq.com>
 * @Date 2025/03/02 17:27:41
 * @Describe PhoneConnectRuleAdapter
 */
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cc.winboll.studio.contacts.R;
import cc.winboll.studio.contacts.beans.PhoneConnectRuleModel;
import cc.winboll.studio.contacts.dun.Rules;
import cc.winboll.studio.contacts.views.LeftScrollView;
import cc.winboll.studio.libappbase.dialogs.YesNoAlertDialog;
import cc.winboll.studio.libappbase.utils.ToastUtils;
import java.util.ArrayList;
import java.util.List;

public class PhoneConnectRuleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final String TAG = "PhoneConnectRuleAdapter";

    private static final int VIEW_TYPE_SIMPLE = 0;
    private static final int VIEW_TYPE_EDIT = 1;

    private Context context;
    private List<PhoneConnectRuleModel> ruleList;

    public PhoneConnectRuleAdapter(Context context, List<PhoneConnectRuleModel> ruleList) {
        this.context = context;
        this.ruleList = ruleList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        if (viewType == VIEW_TYPE_SIMPLE) {
            View view = inflater.inflate(R.layout.view_phone_connect_rule_simple, parent, false);
            return new SimpleViewHolder(parent, view);
        } else {
            View view = inflater.inflate(R.layout.view_phone_connect_rule, parent, false);
            return new EditViewHolder(parent, view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final PhoneConnectRuleModel model = ruleList.get(position);
        if (holder instanceof SimpleViewHolder) {
            final SimpleViewHolder simpleViewHolder = (SimpleViewHolder) holder;
            String szView = model.getRuleText().trim().equals("") ?"[NULL]": model.getRuleText();
            simpleViewHolder.tvRuleText.setText(szView);
            simpleViewHolder.checkBoxAllow.setChecked(model.isAllowConnection());
			simpleViewHolder.checkBoxAllow.setEnabled(false);
            simpleViewHolder.checkBoxEnable.setChecked(model.isEnable());
			simpleViewHolder.checkBoxEnable.setEnabled(false);
            simpleViewHolder.scrollView.setOnActionListener(new LeftScrollView.OnActionListener(){

                    @Override
                    public void onUp() {
                        ArrayList<PhoneConnectRuleModel> list = Rules.getInstance(context).getPhoneBlacRuleBeanList();
                        if (position > 0) {
                            ToastUtils.show("onUp");
                            simpleViewHolder.scrollView.smoothScrollTo(0, 0);
//                            PhoneConnectRuleModel newBean = new PhoneConnectRuleModel();
//                            newBean.setRuleText(list.get(position).getRuleText());
//                            newBean.setIsAllowConnection(list.get(position).isAllowConnection());
//                            newBean.setIsEnable(list.get(position).isEnable());
//                            newBean.setIsSimpleView(list.get(position).isSimpleView());
                            list.add(position - 1, list.get(position));
                            list.remove(position + 1);
                            Rules.getInstance(context).saveRules();
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onDown() {
                        ArrayList<PhoneConnectRuleModel> list = Rules.getInstance(context).getPhoneBlacRuleBeanList();
                        if (position < list.size() - 1) {
                            ToastUtils.show("onDown");
                            simpleViewHolder.scrollView.smoothScrollTo(0, 0);
//                            PhoneConnectRuleModel newBean = new PhoneConnectRuleModel();
//                            newBean.setRuleText(list.get(position).getRuleText());
//                            newBean.setIsAllowConnection(list.get(position).isAllowConnection());
//                            newBean.setIsEnable(list.get(position).isEnable());
//                            newBean.setIsSimpleView(list.get(position).isSimpleView());
                            list.add(position + 2, list.get(position));
                            list.remove(position);
                            Rules.getInstance(context).saveRules();
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onEdit() {
                        simpleViewHolder.scrollView.smoothScrollTo(0, 0);
                        model.setIsSimpleView(false);
                        notifyDataSetChanged();
                        //notifyItemChanged(position);
                    }

                    @Override
                    public void onDelete() {
                        YesNoAlertDialog.show(simpleViewHolder.scrollView.getContext(), "删除确认", "是否删除该通话规则？", new YesNoAlertDialog.OnDialogResultListener(){

                                @Override
                                public void onYes() {
                                    simpleViewHolder.scrollView.smoothScrollTo(0, 0);
                                    model.setIsSimpleView(true);
                                    ArrayList<PhoneConnectRuleModel> list = Rules.getInstance(context).getPhoneBlacRuleBeanList();
                                    list.remove(position);
                                    Rules.getInstance(context).saveRules();
                                    notifyDataSetChanged();
                                    //notifyItemChanged(position);
                                }

                                @Override
                                public void onNo() {
                                }
                            });

                    }
                });
//            simpleViewHolder.editButton.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        model.setIsSimpleView(false);
//                        notifyItemChanged(position);
//                    }
//                });
//            simpleViewHolder.deleteButton.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        model.setIsSimpleView(false);
//                        ArrayList<PhoneConnectRuleModel> list = Rules.getInstance(context).getPhoneBlacRuleBeanList();
//                        list.remove(position);
//                        Rules.getInstance(context).saveRules();
//                        notifyItemChanged(position);
//                    }
//                });
//            // 触摸事件处理
//            simpleViewHolder.contentLayout.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        switch (event.getAction()) {
//                            case MotionEvent.ACTION_DOWN:
//                                simpleViewHolder.startX = event.getX();
//                                simpleViewHolder.isSwiping = true;
//                                break;
//                            case MotionEvent.ACTION_MOVE:
//                                if (simpleViewHolder.isSwiping) {
//                                    float deltaX = simpleViewHolder.startX - event.getX();
//                                    if (deltaX > 0) { // 左滑
//                                        float translationX = Math.max(-simpleViewHolder.actionLayout.getWidth(), -deltaX);
//                                        simpleViewHolder.contentLayout.setTranslationX(translationX);
//                                        simpleViewHolder.actionLayout.setVisibility(View.VISIBLE);
//                                    }
//                                }
//                                break;
//                            case MotionEvent.ACTION_UP:
//                                simpleViewHolder.isSwiping = false;
//                                if (simpleViewHolder.contentLayout.getTranslationX() < -simpleViewHolder.actionLayout.getWidth() / 2) {
//                                    // 保持按钮显示
//                                    simpleViewHolder.contentLayout.setTranslationX(-actionLayout.getWidth());
//                                } else {
//                                    // 恢复原状
//                                    simpleViewHolder.contentLayout.animate().translationX(0).setDuration(200).start();
//                                    simpleViewHolder.actionLayout.setVisibility(View.INVISIBLE);
//                                }
//                                break;
//                        }
//                        return true;
//                    }
//                });
        } else if (holder instanceof EditViewHolder) {
            final EditViewHolder editViewHolder = (EditViewHolder) holder;
            editViewHolder.editText.setText(model.getRuleText());
            editViewHolder.checkBoxAllow.setChecked(model.isAllowConnection());
            editViewHolder.checkBoxEnable.setChecked(model.isEnable());
            editViewHolder.buttonConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        model.setRuleText(editViewHolder.editText.getText().toString());
                        model.setIsAllowConnection(editViewHolder.checkBoxAllow.isChecked());
                        model.setIsEnable(editViewHolder.checkBoxEnable.isChecked());
                        model.setIsSimpleView(true);
                        Rules.getInstance(context).saveRules();
                        notifyItemChanged(position);
                        Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
                    }
                });
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        PhoneConnectRuleModel model = ruleList.get(position);
        // 这里可以根据模型的状态来决定视图类型，简单起见，假设点击按钮后进入编辑视图
        return model.isSimpleView() ? VIEW_TYPE_SIMPLE : VIEW_TYPE_EDIT;
    }

    static class SimpleViewHolder extends RecyclerView.ViewHolder {

        private final LeftScrollView scrollView;
        private final TextView tvRuleText;
		CheckBox checkBoxAllow;
        CheckBox checkBoxEnable;


        public SimpleViewHolder(@NonNull ViewGroup parent, @NonNull View itemView) {
            super(itemView);
            scrollView = itemView.findViewById(R.id.scrollView);
			LayoutInflater inflater = LayoutInflater.from(itemView.getContext());
		    View viewContent = inflater.inflate(R.layout.view_phone_connect_rule_simple_content, parent, false);
            tvRuleText = viewContent.findViewById(R.id.ruletext_tv);
            checkBoxAllow = viewContent.findViewById(R.id.checkbox_allow);
            checkBoxEnable = viewContent.findViewById(R.id.checkbox_enable);
            //tvRuleText = new TextView(itemView.getContext());
            scrollView.setContentWidth(parent.getWidth());
            //scrollView.setContentWidth(600);
            scrollView.addContentLayout(viewContent);
        }

    }

    static class EditViewHolder extends RecyclerView.ViewHolder {
        EditText editText;
        CheckBox checkBoxAllow;
        CheckBox checkBoxEnable;
        Button buttonConfirm;

        public EditViewHolder(@NonNull ViewGroup parent, @NonNull View itemView) {
            super(itemView);
            editText = itemView.findViewById(R.id.edit_text);
            checkBoxAllow = itemView.findViewById(R.id.checkbox_allow);
            checkBoxEnable = itemView.findViewById(R.id.checkbox_enable);
            buttonConfirm = itemView.findViewById(R.id.button_confirm);
        }
    }

	private void setCheckBoxTouchListener(CheckBox checkBox) {

	}
}

