package com.tenlionsoft.baselib.core.widget.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tenlionsoft.baselib.R;

/**
 * 作者: adam
 * 日期: 2020/6/15 - 2:17 PM
 * 邮箱: itgaojian@163.com
 * 描述:
 */
public class CustomDialog extends ProgressDialog {
    private String ms;
    private Context mContext;

    public CustomDialog(Context context, String message) {
        super(context, R.style.dialog_full_screen_no_deep_progress);
        this.ms = message;
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(getContext(), ms);
    }

    private void init(Context context, String message) {
        //设置不可取消，点击其他区域不能取消，实际中可以抽出去封装供外包设置
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        View cunstomView = LayoutInflater.from(context).inflate(R.layout.layout_custom_loading_dialog, null);
        TextView mtv = cunstomView.findViewById(R.id.tv_load_dialog);
        mtv.setText(message);
        setContentView(cunstomView);
        setDialogParams();
    }

    /**
     * 设置dialog参数
     */
    private void setDialogParams() {
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Dialog_ChoseFile);
        WindowManager.LayoutParams params = window.getAttributes();
        params.dimAmount = 0.3f;
        params.width = (int) (width * 0.5);
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }

    @Override
    public void show() {
        super.show();
    }
}
