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

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
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;
import android.widget.TextView;

import com.tenlionsoft.baselib.R;


/**
 * 作者 : Adam on 2018/7/23.
 * 邮箱 : itgaojian@163.com
 * 描述 : 是否升级的Dialog
 */
public class UpdateDialogView extends Dialog {

    private boolean iscancelable;
    private boolean isBackCancelable;
    private View view;
    private Context context;
    private int gravity;
    private OnChoseListener mListener;
    private String content;

    private UpdateDialogView(Context context, boolean isCancelable, boolean isBackCancelable, int gravity, String content) {
        super(context, R.style.Dialog_ChoseFile);
        this.context = context;
        this.gravity = gravity;
        this.iscancelable = isCancelable;
        this.isBackCancelable = isBackCancelable;
        this.content = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_check_update, null);
        setContentView(view);
        view.findViewById(R.id.btn_confirm).setOnClickListener(v -> choseType());
        view.findViewById(R.id.btn_cancel).setOnClickListener(v -> this.dismiss());
        TextView mTvContent = view.findViewById(R.id.tv_update_content);
        mTvContent.setText(content);
        setCancelable(iscancelable);
        setCanceledOnTouchOutside(isBackCancelable);
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        //弹出动画
        //window.setWindowAnimations(R.style.Dialog_ChoseFile);
        WindowManager.LayoutParams params = window.getAttributes();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        params.height = (int) (d.getHeight() * 0.3);
        params.width = (int) (d.getWidth() * 0.7);
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    public void addOnChoseListener(OnChoseListener listener) {
        this.mListener = listener;
    }

    /**
     * 选择
     */
    private void choseType() {
        if (mListener != null) {
            mListener.downLoad();
        }
        this.dismiss();
    }

    public interface OnChoseListener {
        void downLoad();
    }

    public static class DialogBuilder {
        private Context mContext;
        private boolean isCancelable;
        private boolean mIsBackable;
        private int mGravity = Gravity.BOTTOM;
        private String content;
        private OnChoseListener mListener;

        public DialogBuilder(Context context) {
            this.mContext = context;
        }

        public DialogBuilder setIscancelable(boolean iscancelable) {
            this.isCancelable = iscancelable;
            return this;
        }

        public DialogBuilder setIsBackCancelable(boolean isBackCancelable) {
            this.mIsBackable = isBackCancelable;
            return this;
        }

        public DialogBuilder setShowLocation(int gravity) {
            this.mGravity = gravity;
            return this;
        }

        public DialogBuilder setUpdateContent(String content) {
            this.content = content;
            return this;
        }

        public UpdateDialogView build() {
            return new UpdateDialogView(this.mContext, this.isCancelable, this.mIsBackable, this.mGravity, content);
        }
    }
}