package com.duben.microtribe.ui.widgets;

import android.content.Context;
import android.os.CountDownTimer;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.duben.microtribe.R;

/**
 * 仿苹果自定义dialog选择框
 */
public class CustomDialogAsApple extends BaseDialog {
    private Context context;
    private LayoutParams lp;
    private DialogListener listener;

    private TextView dialog_tv_title, dialog_tv_content;
    private Button dialog_btn_left, dialog_btn_right;
    private View dialog_v_line;

    public CustomDialogAsApple(Context context) {
        this(context, null);
    }

    public CustomDialogAsApple(Context context, DialogListener listener) {
        super(context, R.style.dialog);
        this.context = context;
        this.listener = listener;
        if (this.listener != null) {
            this.listener.setDialog(this);
        }
        setContentView(R.layout.dialog_main);
        // 设置window属性
        lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = LayoutParams.MATCH_PARENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        lp.windowAnimations = R.style.DialogAnimBottom;
        getWindow().setAttributes(lp);

        // 设置外部不可关闭
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setOnKeyListener((dialog, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK);

        // 查找View
        dialog_tv_title = findViewById(R.id.dialog_tv_title);
        dialog_tv_content = findViewById(R.id.dialog_tv_content);
        dialog_tv_content.setMovementMethod(ScrollingMovementMethod.getInstance());
        dialog_btn_left = findViewById(R.id.dialog_btn_left);
        dialog_btn_right = findViewById(R.id.dialog_btn_right);
        dialog_v_line = findViewById(R.id.dialog_v_line);

        initListener();
    }

    private void initListener() {
        if (listener != null) {
            dialog_btn_left.setOnClickListener(listener);
            dialog_btn_right.setOnClickListener(listener);
        }
    }

    public void setDialogListener(DialogListener listener) {
        this.listener = listener;
    }


    /**
     * Dialog显示标题
     *
     * @param s
     */
    public void setTitle(String s) {
        dialog_tv_title.setVisibility(View.VISIBLE);
        dialog_tv_title.setText(s);
    }

    /**
     * Dialog显示标题
     *
     * @param size
     */
    public void setTitleSize(int size) {
        dialog_tv_title.setTextSize(size);
    }

    /**
     * Dialog显示内容
     *
     * @param s
     */
    public void setContent(String s) {
        dialog_tv_content.setText(s);
    }

    public void setContent(Spanned s) {
        dialog_tv_content.setText(s);
    }

    /**
     * Dialog显示content文字
     *
     * @param color
     */
    public void setContentColor(int color) {
        dialog_tv_content.setTextColor(ContextCompat.getColor(context, color));
    }

    /**
     * content文字大小
     *
     * @param size
     */
    public void setContentSize(int size) {
        dialog_tv_content.setTextSize(size);
    }

    public void setContentGravity(int gravity) {
        dialog_tv_content.setGravity(gravity);
    }

    public void setLeftClickListener(DialogListener listener) {
        dialog_btn_left.setOnClickListener(listener);
    }

    public void setRightClickListener(DialogListener listener) {
        dialog_btn_right.setOnClickListener(listener);
    }

    /**
     * Dialog显示left按钮文字
     *
     * @param s
     */
    public void setLeft(String s) {
        dialog_btn_left.setText(s);
        if (TextUtils.isEmpty(dialog_btn_right.getText())) {
            dialog_btn_right.setVisibility(View.GONE);
            dialog_v_line.setVisibility(View.GONE);
        } else {
            dialog_btn_left.setVisibility(View.VISIBLE);
            dialog_v_line.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 只显示一个按钮
     *
     * @param
     */
    public void setSingleBtn() {
        dialog_btn_left.setVisibility(View.VISIBLE);
        dialog_v_line.setVisibility(View.GONE);
        dialog_btn_right.setVisibility(View.GONE);
    }

    /**
     * Dialog显示left按钮文字颜色
     *
     * @param color
     */
    public void setLeftColor(int color) {
        dialog_btn_left.setTextColor(ContextCompat.getColor(context, color));
    }

    /**
     * Dialog显示right按钮文字
     *
     * @param s
     */
    public void setRight(String s) {
        dialog_btn_right.setText(s);
        if (TextUtils.isEmpty(dialog_btn_left.getText())) {
            dialog_btn_left.setVisibility(View.GONE);
            dialog_v_line.setVisibility(View.GONE);
        } else {
            dialog_btn_right.setVisibility(View.VISIBLE);
            dialog_v_line.setVisibility(View.VISIBLE);
        }
    }

    /**
     * Dialog显示right按钮文字
     *
     * @param s
     */
    public void setRightDelay(String s, int second) {
        dialog_btn_right.setText(s);
        if (TextUtils.isEmpty(dialog_btn_left.getText())) {
            dialog_btn_left.setVisibility(View.GONE);
            dialog_v_line.setVisibility(View.GONE);
        } else {
            dialog_btn_right.setVisibility(View.VISIBLE);
            dialog_v_line.setVisibility(View.VISIBLE);

            dialog_btn_right.setEnabled(false);

            CountDownTimer countDownTimer = new CountDownTimer(second * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    String value = String.valueOf((int) ((millisUntilFinished / 1000) + 1));
                    dialog_btn_right.setText(s + "(" + value + ")");
                }

                @Override
                public void onFinish() {
                    dialog_btn_right.setText(s);

                    dialog_btn_right.setEnabled(true);
                }
            };
            countDownTimer.start();
        }
    }

    /**
     * Dialog显示right按钮文字颜色
     *
     * @param color
     */
    public void setRightColor(int color) {
        dialog_btn_right.setTextColor(ContextCompat.getColor(context, color));
    }
}
