package com.example.iot_controlhost.ui.dialog;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogNumberInputBinding;

/**
 * @Description 数字输入弹窗
 * @Author DuanKaiji
 * @CreateTime 2024年03月12日 17:46
 */
public class NumberInputDialog extends BaseDialog<DialogNumberInputBinding> {
    TextView tv;
    private int lastAction;
    private float startX, startY;


    public NumberInputDialog(@NonNull Context context, TextView tv) {
        super(context);
        this.tv = tv;
        // 初始化界面控件 tv文本末尾闪烁“|”
        tv.setText(getAfterText(tv.getText().toString()));
        binding.btnPoint.setOnClickListener(getNumberOnClickListener(tv, -1));
        binding.btn0.setOnClickListener(getNumberOnClickListener(tv, 0));
        binding.btn1.setOnClickListener(getNumberOnClickListener(tv, 1));
        binding.btn2.setOnClickListener(getNumberOnClickListener(tv, 2));
        binding.btn3.setOnClickListener(getNumberOnClickListener(tv, 3));
        binding.btn4.setOnClickListener(getNumberOnClickListener(tv, 4));
        binding.btn5.setOnClickListener(getNumberOnClickListener(tv, 5));
        binding.btn6.setOnClickListener(getNumberOnClickListener(tv, 6));
        binding.btn7.setOnClickListener(getNumberOnClickListener(tv, 7));
        binding.btn8.setOnClickListener(getNumberOnClickListener(tv, 8));
        binding.btn9.setOnClickListener(getNumberOnClickListener(tv, 9));
        binding.btnDelete.setOnClickListener(view -> {
            String text = tv.getText().toString();
            if (text.length() > 1){
                tv.setText(getAfterText(text.substring(0, text.length() - 2)));
            }
        });
        binding.clClose.setOnClickListener(view -> dismiss());
        // Make the dialog draggable
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.x = 0;
        params.y = 0;
        getWindow().setAttributes(params);
        binding.cardRoot.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        lastAction = MotionEvent.ACTION_DOWN;
                        startX = event.getRawX() - params.x;
                        startY = event.getRawY() - params.y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        lastAction = MotionEvent.ACTION_MOVE;
                        params.x = (int) (event.getRawX() - startX);
                        params.y = (int) (event.getRawY() - startY);
                        getWindow().setAttributes(params);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (lastAction == MotionEvent.ACTION_DOWN) {
                            // Handle click event if necessary
                        }
                        break;
                    default:
                        return false;
                }
                return true;
            }
        });
    }

    /**
     * 获取密码输入弹窗的
     * 数字事件
     *
     * @param ti     输入框
     * @param number 目标数字 0-10
     * @return 点击事件
     */
    private View.OnClickListener getNumberOnClickListener(TextView ti, int number) {
        if (number == -1) {
            return view -> {
                if (!ti.getText().toString().contains(".")) {
                    ti.setText(getAfterText(getTrulyText() + "."));
                }
            };
        } else {
            return view -> ti.setText(getAfterText(getTrulyText() + number));
        }
    }

    /**
     * 获取真实的文本
     */
    private String getTrulyText() {
        return tv.getText().toString().substring(0, tv.getText().length() - 1);
    }

    /**
     * 获取处理后的文本
     */
    private String getAfterText(String text) {
        return text + "|";
    }

    @Override
    public void dismiss() {
        super.dismiss();
        tv.setText(getTrulyText());
    }
}
