package com.unis.sxzcl.widget.dialog;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.unis.sxzcl.R;
import com.unis.sxzcl.databinding.DialogVersionBinding;

/**
 * 包名: com.unis.sxzcl.widget.dialog
 * 类名: VersionDialog
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/25 10:44
 * 更新人:
 * 更新时间: 2020/12/25 10:44
 * 更新说明:
 * 版本号: 1.0
 */
public class VersionDialog {

    Context mContext;
    private SystemDialog mDialog;

    private UpdateCallBack mUpdateCallBack;
    private InstallCallback mInstallCallback;
    private DialogVersionBinding mBinding;

    public VersionDialog(@NonNull Context context) {
        mContext = context;
        initDialog();
    }

    /**
     * 初始化对话框
     */
    private void initDialog() {
        mDialog = new SystemDialog(mContext)
                .setHideTitle()
                .setHideClose()
                .setContentView(getView())
                .setOnShowListener(dialog -> {
                })
                .setDismissListener(dialog -> {
                });

    }

    /**
     * 获取自定义布局
     */
    private View getView() {
        mBinding = DialogVersionBinding.inflate(LayoutInflater.from(mContext));

        showInit();

        // 更新按钮点击事件
        mBinding.tvUpdate.setOnClickListener(v -> {
            if (mUpdateCallBack != null)
                mUpdateCallBack.onUpdateListener(this);
        });

        // 安装按钮点击事件
        mBinding.tvInstall.setOnClickListener(v -> {
            if (mInstallCallback != null)
                mInstallCallback.onInstallListener(this);
        });
        return mBinding.getRoot();
    }

    /**
     * 显示初始状态
     */
    public VersionDialog showInit() {
        // 隐藏进度
        mBinding.layoutProgress.setVisibility(View.GONE);
        // 隐藏右侧卡通图片
        mBinding.ivRight.setVisibility(View.GONE);
        // 隐藏安装
        mBinding.tvInstall.setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置版本描述
     */
    public VersionDialog setContent(String content) {
        mBinding.tvContent.setText(content);
        return this;
    }

    /**
     * 设置版本名称
     */
    public VersionDialog setVersionName(String versionName) {
        mBinding.tvVersionName.setText(versionName);
        return this;
    }

    /**
     * 设置进度
     */
    public VersionDialog setProgress(int progress) {
        // 刷新进度时
        // 隐藏顶部背景
        mBinding.ivTop.setVisibility(View.INVISIBLE);
        // 隐藏更新按钮
        mBinding.tvUpdate.setVisibility(View.GONE);
        // 隐藏安装
        mBinding.tvInstall.setVisibility(View.GONE);

        // 显示右侧卡通图片
        mBinding.ivRight.setVisibility(View.VISIBLE);
        // 文字改为主题色
        mBinding.tvVersionName.setTextColor(ContextCompat.getColor(mContext, R.color.colorTheme));
        mBinding.t1.setTextColor(ContextCompat.getColor(mContext, R.color.colorTheme));

        // 显示进度
        mBinding.layoutProgress.setVisibility(View.VISIBLE);
        mBinding.progressBar.setMax(100);
        // 设置进度
        mBinding.tvProgress.setText(progress + "%");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mBinding.progressBar.setProgress(progress, true);
        } else {
            mBinding.progressBar.setProgress(progress);
        }
        return this;
    }

    /**
     * 显示安装按钮
     */
    public VersionDialog showInstall() {
        mBinding.layoutProgress.setVisibility(View.GONE);
        mBinding.tvUpdate.setVisibility(View.GONE);
        mBinding.tvInstall.setVisibility(View.VISIBLE);
        return this;
    }


    /**
     * 设置更新按钮监听事件
     */
    public VersionDialog setUpdateListener(UpdateCallBack updateListener) {
        mUpdateCallBack = updateListener;
        return this;
    }

    /**
     * 设置安装按钮监听事件
     */
    public VersionDialog setInstallCallback(InstallCallback installCallback) {
        mInstallCallback = installCallback;
        return this;
    }

    /**
     * 显示对话框
     */
    public VersionDialog show() {
        mDialog.show();
        return this;
    }

    /**
     * 隐藏对话框
     */
    public VersionDialog dismiss() {
        mDialog.dismiss();
        return this;
    }

    /**
     * 是否可关闭窗口
     *
     * @return
     */
    public VersionDialog setCancelable(boolean cancelable) {
        mDialog.setCancelable(cancelable);
        return this;
    }

    /**
     * 获取对话框
     *
     * @return
     */
    public SystemDialog getDialog() {
        return mDialog;
    }

    /**
     * 更新按钮接口
     */
    public interface UpdateCallBack {
        void onUpdateListener(VersionDialog dialog);
    }

    /**
     * 安装按钮接口
     */
    public interface InstallCallback {
        void onInstallListener(VersionDialog dialog);
    }
}
