package com.hw.nativeapp.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.hw.nativeapp.R;

import java.math.BigDecimal;


public class Dialog_UpdateProgress extends Dialog {

    public Dialog_UpdateProgress(Context mcontext) {
        super(mcontext);
    }

    public Dialog_UpdateProgress(Context context, int themeResId){
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;

        public Builder(Context context) {
            this.context = context;
        }



        public void setProgressBar(int soFarBytes, int totalBytes){
            if (progressBar != null && totalBytes > 0){
                BigDecimal proDecimal = BigDecimal.valueOf(soFarBytes);
                BigDecimal totalDecimal = BigDecimal.valueOf(totalBytes);
                int progress = proDecimal.divide(totalDecimal,2,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).intValue();
                progressBar.setProgress(progress);
                txProgresspercentage.setText(progress + "%");
            }else {
                progressBar.setProgress(0);
                txProgresspercentage.setText("0%");
            }
        }

        public void setTxProgress(int soFarBytes, int totalBytes ){
            if (txProgress != null){
                if (totalBytes < soFarBytes){
                    txProgress.setText(byteToMB(soFarBytes) + "/" + byteToMB(soFarBytes));
                }else {
                    txProgress.setText(byteToMB(soFarBytes) + "/" + byteToMB(totalBytes));
                }
            }
        }


        ProgressBar progressBar;
        TextView txProgress;
        TextView txProgresspercentage;


        public Dialog_UpdateProgress create() {
            final Dialog_UpdateProgress dialog = new Dialog_UpdateProgress(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_updateprogress, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
//            dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
            txProgress = (TextView) view.findViewById(R.id.tx_progress);
            txProgresspercentage = (TextView) view.findViewById(R.id.tx_progresspercentage);

//            etContent.setText(message);
//            btnupCancel.setText(negative_text);
//            btnupEnter.setText(positive_text);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            btnupCancel.setOnClickListener(v -> negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE));
//            btnupEnter.setOnClickListener(v -> positiveListener.onClick(dialog, Dialog.BUTTON_POSITIVE));
            return dialog;
        }

        private String byteToMB(long size){
            long kb = 1024;
            long mb = kb*1024;
            long gb = mb*1024;
            if (size >= gb){
                return String.format("%.1f GB",(float)size/gb);
            }else if (size >= mb){
                float f = (float) size/mb;
                return String.format(f > 100 ?"%.0f MB":"%.1f MB",f);
            }else if (size > kb){
                float f = (float) size / kb;
                return String.format(f>100?"%.0f KB":"%.1f KB",f);
            }else {
                return String.format("%d B",size);
            }
        }
    }


}
