package com.zhaohaoting.framework.abs.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.zhaohaoting.framework.R;


/**
 * author：Created by ZHT on 2017/11/21 17:03
 * email：526309416@qq.com
 * desc：
 */
public abstract class BaseDialog extends Dialog {

    protected Context context;
    protected FrameLayout llContent;
    protected Bundle savedInstanceState;

    public BaseDialog(@NonNull Context context) {
        this(context, R.style.dialog_style);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        setContentView(R.layout.dialog_base_jiulong);
        setDialogSize();
        llContent = findViewById(R.id.ll_content);
        ViewDataBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(context), getLayoutRes(), llContent, true);
        initContentView(viewDataBinding);
    }

    protected abstract void initContentView(ViewDataBinding viewDataBinding);

    /**
     * 设置dialog的大小
     */
    protected void setDialogSize() {
        WindowManager m = ((Activity) context).getWindowManager();
        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用/
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
//        p.y = -160;
        dialogWindow.setAttributes(p);
    }

    protected abstract int getLayoutRes();
}
