package com.stm.asset.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.stm.asset.R;
import com.stm.asset.utils.ViewUtil;

/**
 * 类名称：CommonAlertDialog
 * 类描述：基本提示弹出框
 * 创建人：story
 * 创建时间：2021-09-04 15:38
 */
public class CommonAlertDialog extends Dialog implements android.view.View.OnClickListener {

    private TextView titleTxt, contentTxt, sureTxt, cancelTxt;
    private float widthScale = 0f;
    private LinearLayout.LayoutParams params;
    private Context context;

    public CommonAlertDialog(Context context){
        super(context,R.style.BaseDialog);
        this.context=context;
    }

    public CommonAlertDialog(Context context,float widthScale ){
        super(context,R.style.BaseDialog);
        this.context=context;
        this.widthScale=widthScale;
    }

    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(this.context).inflate(R.layout.view_common_dialog, null);
        titleTxt = view.findViewById(R.id.common_dialog_title_txt);
        contentTxt = view.findViewById(R.id.common_dialog_content_txt);
        sureTxt = view.findViewById(R.id.common_dialog_sure_txt);
        cancelTxt = view.findViewById(R.id.common_dialog_cancel_txt);
        cancelTxt.setOnClickListener(this);
        sureTxt.setOnClickListener(this);
        if (widthScale > 0) {
            Window dialogWindow = this.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            this.getWindow().setAttributes(lp);
            int dialog_width = (int) (ViewUtil.getScreenWidth(context) * widthScale);
            view.setMinimumHeight(dialog_width/2);
            this.params=new LinearLayout.LayoutParams(dialog_width, ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        if(this.params==null)
            this.setContentView(view);
        else
            this.setContentView(view, this.params);
    }

    /**
     * 设置内容
     */
    public void setText(String content){
        this.contentTxt.setText(content);
    }

    /**
     * 隐藏标题
     */
    public void hadeTitleLayout(){
        this.titleTxt.setVisibility(View.GONE);
    }

    /**
     * 设置标题和内容
     */
    public void setText(String title,String content){
        this.titleTxt.setText(title);
        this.contentTxt.setText(content);
    }

    /**
     * 设置标题、内容和确定按钮内容
     */
    public void setText(String title, String content, String sure){
        this.titleTxt.setText(title);
        this.contentTxt.setText(content);
        this.sureTxt.setText(sure);
    }

    /**
     * 设置标题、内容、确定和取消按钮内容
     */
    public void setText(String title, String content, String sure, String cancel){
        this.titleTxt.setText(title);
        this.contentTxt.setText(content);
        this.sureTxt.setText(sure);
        this.cancelTxt.setText(cancel);
    }

    public void setSureListener(View.OnClickListener listener){
        this.sureTxt.setOnClickListener(listener);
    }

    public void setCancelListener(View.OnClickListener listener){
        this.cancelTxt.setOnClickListener(listener);
    }

    public void hideCancelBtn() {
        this.cancelTxt.setVisibility(View.GONE);
        findViewById(R.id.common_dialog_devices_line).setVisibility(View.GONE);
    }

    public void setViewParams(LinearLayout.LayoutParams params){
        this.params=params;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.common_dialog_cancel_txt:
                this.dismiss();
                break;
        }
    }

}
