package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;


import com.tofly.ghrq_route.R;

import java.text.MessageFormat;

public class DownloadDialogProgressUtil {
    private Context mContext;
    private ProgressBar mProgressBar;//进度条
    private TextView textTitle;
    private TextView textFileName;
    private TextView textState;
    private TextView textProgress;
    private AlertDialog mDownloadDialog;//对话框

    private static DownloadDialogProgressUtil instance;

    public static DownloadDialogProgressUtil getInstance(){
        if(instance == null){
            instance = new DownloadDialogProgressUtil();
        }
        return instance;
    }

    /*
     * 显示正在下载对话框
     */
    public void showDownloadDialog(Context context, String title) {
        mContext = context;

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_progress, null);
        textTitle = view.findViewById(R.id.tv_dialog_title);
        textTitle.setText(mContext.getResources().getString(R.string.downloading));
        if (title != null){
            textTitle.setText(title);
        }

        textFileName = view.findViewById(R.id.text_file_name);
        mProgressBar = view.findViewById(R.id.myprogress);
        textProgress = view.findViewById(R.id.progressbar_tv1);
        textState = view.findViewById(R.id.progressbar_tv2);
        builder.setView(view);

        // 设置按钮(左)
       /* builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 隐藏当前对话框
                dialog.dismiss();
                // 设置下载状态为取消
                mIsCancel = true;
            }
        });*/
        // 设置按钮(右)
        builder.setNegativeButton(mContext.getResources().getString(R.string.close), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        mDownloadDialog = builder.create();
        mDownloadDialog.setCancelable(false);
        mDownloadDialog.setCanceledOnTouchOutside(false);
        mDownloadDialog.show();
        mDownloadDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.INVISIBLE);
    }

    public void updateDownloadProgress(int downloaded, int total, int progress, String fileName){
        if (mDownloadDialog != null && mDownloadDialog.isShowing()){
            if (total >=0 && downloaded >= 0){
                mProgressBar.setIndeterminate(false);
                mProgressBar.setProgress(progress);
                textProgress.setText(MessageFormat.format("{0}%", progress));

                textState.setText(String.format("第%d个 / 共%d个", downloaded, total));
            }else {
                mProgressBar.setIndeterminate(true);
            }
            //textTitle.setText(mContext.getResources().getString(R.string.downloading));
            textFileName.setText(fileName);
            mDownloadDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.INVISIBLE);
        }
    }

    public void updateDownloadFinish(String fileName, int result){
        if (mDownloadDialog != null && mDownloadDialog.isShowing()){
            textFileName.setText(fileName);
            if (result == 1){
                textTitle.setText(mContext.getResources().getString(R.string.downloaded));
            }else {
                textTitle.setText(mContext.getResources().getString(R.string.download_failed));
            }
            mDownloadDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
                    .setVisibility(View.VISIBLE);
        }
    }
}
