package com.dankal.dankal_launcher;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Author: roczheng
 * Date: 2020/7/6
 * Time: 10:49
 * Description:
 */
public class CustomDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private int[] mListenedItem;
    private int mResource;
    private View mDialogView;

    //默认比例
    private int mScreenWidth = 4;
    private int mDialogWidth = 3;

    public CustomDialog(Context context, int resource, int[] listenedItem) {
        super(context, R.style.NormalDialogStyle);//加载dialog的样式
        this.mContext = context;
        this.mListenedItem = listenedItem;
        this.mResource = resource;
    }

    public CustomDialog(Context context, View view, int[] listenedItem) {
        super(context, R.style.NormalDialogStyle);//加载dialog的样式
        this.mContext = context;
        this.mListenedItem = listenedItem;
        this.mDialogView = view;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mDialogView != null) {
            setContentView(mDialogView);
        } else if (mResource != 0) {
            mDialogView = LayoutInflater.from(mContext).inflate(mResource, null);
            setContentView(mDialogView);
        }

        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);//设置dialog显示居中
        //dialogWindow.setWindowAnimations();设置动画效果
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth() * mDialogWidth / mScreenWidth;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);
        for (int id : mListenedItem) {
            findViewById(id).setOnClickListener(this);
        }
    }

    public View getDialogView() {
        return mDialogView;
    }

    private OnCenterItemClickListener listener;

    public interface OnCenterItemClickListener {
        void OnCenterItemClick(CustomDialog dialog, View view);
    }

    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        dismiss();//注意：我在这里加了这句话，表示只要按任何一个控件的id,弹窗都会消失，不管是确定还是取消。
        if (listener != null)
            listener.OnCenterItemClick(this, v);
    }

    /**
     * 设置屏幕宽度和对话框的比例
     *
     * @param screenWidth
     * @param dialogWidth
     */
    public void setProportion(int screenWidth, int dialogWidth) {
        this.mScreenWidth = screenWidth;
        this.mDialogWidth = dialogWidth;
    }
}
