package com.bbitcn.silk.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bbitcn.silk.R;
import com.google.android.material.progressindicator.LinearProgressIndicator;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;
import com.xuexiang.rxutil2.rxjava.task.RxUITask;

public class DialogUtil {
    private static Dialog progressDialog;
    private static TextView textViewInfo;

    /**
     * 显示加载中弹窗，支持中途改变提示文字
     *
     * @param context 上下文
     * @param info    提示信息
     */
    public static void showLoadingDialog(Context context, String info) {
        if (progressDialog != null) {
            progressDialog.dismiss(); // 如果之前有Dialog显示，则先关闭
            progressDialog = null; // 释放旧Dialog
        }
        progressDialog = new Dialog(context, R.style.MyDialogTheme);
        progressDialog.setContentView(R.layout.dialog_loading);
        progressDialog.setCancelable(false);
        textViewInfo = progressDialog.findViewById(R.id.tv_loading);
        textViewInfo.setText(info);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(progressDialog.getWindow().getAttributes());
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        layoutParams.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
        progressDialog.getWindow().setAttributes(layoutParams);
        progressDialog.show();
    }

    /**
     * 显示加载中弹窗
     * 在IO线程中使用
     *
     * @param context 上下文
     * @param info    提示信息
     */
    public static void showLoadingDialogInIO(Context context, String info) {
        RxJavaUtils.doInUIThread(new RxUITask<Object>(null) {
            @Override
            public void doInUIThread(Object o) {
                showLoadingDialogInIO(context, info);
            }
        });
    }

    /**
     * 关闭加载中弹窗
     * 假设都在UI线程调用
     */
    public static void hideLoadingDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    /**
     * 显示进度条弹窗
     *
     * @param context 上下文
     * @param info    提示信息
     */
    public static void showProgressIndicatorDialog(Context context, String info) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            progressDialog = new Dialog(context, R.style.MyDialogTheme);
            progressDialog.setContentView(R.layout.dialog_process_indicator);
            progressDialog.setCancelable(false);
            textViewInfo = progressDialog.findViewById(R.id.tv_loading);
            progressIndicator = progressDialog.findViewById(R.id.progress_indicator);
            // 设置对话框位置
            Window window = progressDialog.getWindow();
            if (window != null) {
                // 设置对话框位置为屏幕顶部
                window.setGravity(Gravity.CENTER);
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
                layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(layoutParams);
            }
            textViewInfo.setText(info);
            progressDialog.show();
        } else {
            showLoadingDialog(context, info);
        }
    }

    private static LinearProgressIndicator progressIndicator;


    /**
     * 修改进度条弹窗
     *
     * @param info     提示信息
     * @param progress 进度
     */
    public static void setProgressIndicatorDialog(String info, int progress) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            textViewInfo = progressDialog.findViewById(R.id.tv_loading);
            progressIndicator = progressDialog.findViewById(R.id.progress_indicator);
            textViewInfo.setText(info);
            // 设置进度
            progressIndicator.setProgress(progress, true);
        }
    }

    /**
     * 关闭进度条弹窗
     */
    public static void hideProgressIndicatorDialog() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
        } else {
            hideLoadingDialog();
        }
    }

}