package com.renrenliao.im.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.renrenliao.im.R;
import com.renrenliao.im.provider.ClarityPotion;
import com.renrenliao.im.utils.DeviceUtils;
import com.renrenliao.im.utils.LogUtils;
import com.renrenliao.im.utils.StringUtils;

import java.util.Objects;

/**
 * *@ClassName ExplainDialog
 * *@Description
 * *@Author gan
 * *@Date 2022/6/14 15:37
 **/
public class ExplainDialog extends Dialog {

    private TextView tv_title;
    private TextView tv_desc;
    private TextView tv_no, tv_yes;

    private String mTitle;
    private String mDesc;
    private String mNo;
    private String mYes;

    public interface NoListener{
        void setOnNo();
    }

    private NoListener noListener;

    public interface YesListener{
        void setOnYes();
    }
    private YesListener yesListener;
   // private View.OnClickListener yesListener;


    public ExplainDialog() {
        super(Objects.requireNonNull(ClarityPotion.Companion.currentActivity()), R.style.CustomDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_explain);
        Window window = Objects.requireNonNull(getWindow());
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = (int) DeviceUtils.dip2px(ClarityPotion.Companion.currentActivity(), 280f);
        window.setAttributes(layoutParams);
        setCanceledOnTouchOutside(false);
        setCancelable(true);

        tv_title = findViewById(R.id.tv_title);
        tv_desc = findViewById(R.id.tv_desc);
        tv_no = findViewById(R.id.tv_no);
        tv_yes = findViewById(R.id.tv_yes);
    }

    @Override
    public void show() {
        super.show();
        if (!StringUtils.isEmpty(mTitle)) {
            tv_title.setText(mTitle);
        }
        if (!StringUtils.isEmpty(mDesc)) {
            tv_desc.setText(mDesc);
        }
        if (!StringUtils.isEmpty(mNo)){
            tv_no.setText(mNo);
            tv_no.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (noListener!=null){
                        noListener.setOnNo();
                    }
                }
            });
            tv_no.setVisibility(View.VISIBLE);
        }
        if (!StringUtils.isEmpty(mYes)){
            tv_yes.setText(mYes);
            tv_yes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (yesListener!=null){
                        yesListener.setOnYes();
                    }
                }
            });
            tv_yes.setVisibility(View.VISIBLE);
        }
        if (StringUtils.isEmpty(mNo)){
            tv_no.setVisibility(View.GONE);
        }
        if (StringUtils.isEmpty(mYes)){
            tv_yes.setVisibility(View.GONE);
        }
    }

    public static class Builder {
        private ExplainDialog mDialog;

        public Builder() {
            mDialog = new ExplainDialog();
        }

        public Builder setTitle(String title) {
            mDialog.mTitle = title;
            return this;
        }

        public Builder setDesc(String desc) {
            mDialog.mDesc = desc;
            return this;
        }

        public Builder setNo(String no) {
            mDialog.mNo = no;
            return this;
        }

        public Builder setYes(String yes) {
            mDialog.mYes = yes;
            return this;
        }

        public Builder setNoListener(NoListener noListener){
            mDialog.noListener = noListener;
            return this;
        }
        public Builder setYesListener(YesListener yesListener){
            mDialog.yesListener = yesListener;
            return this;
        }

        public ExplainDialog create() {
            return mDialog;
        }


    }

}
