package com.handset.serialportsensor.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.handset.serialportsensor.R;
import com.handset.serialportsensor.activity.BaseActivity;
import com.handset.serialportsensor.utils.ToastHelper;

/**
 * Created by zy on 2019/3/7.
 */

public class ExportProgressDialog {

    private TextView tvContent;
    private Button mButton;
    public AlertDialog mAlertDialog;

    private String ss;

    public AlertDialog showExportDialogNoShow(BaseActivity context, final ToastHelper.OnDialogSubmitClickListener listener, int count, int remain, String ss, String title) {
        this.ss = ss;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.dialog_exporting_test_data, (null));
        mAlertDialog = builder.create();
        mAlertDialog.setView(view);
        mAlertDialog.setCancelable(false);
        tvContent = view.findViewById(R.id.content);
        String text = (count == 0 && remain == 0) ? (ss + "······") : (ss + "(" + (count - remain) + "/" + count + ")······");
        tvContent.setText(text);

        ((TextView) (view.findViewById(R.id.title))).setText(title);

        mButton = view.findViewById(R.id.btn_confirm);
        mButton.setOnClickListener((View view2) -> {
            mAlertDialog.dismiss();
            listener.onOKClickListener(view2);
        });
        return mAlertDialog;
    }

    public boolean update(Activity activity, final int count, final int remain) {
        if (mAlertDialog == null) {
            return false;
        }
        activity.runOnUiThread(() -> {
            if (tvContent != null) {
                String text = ss + "(" + (count - remain) + "/" + count + ")······";
                tvContent.setText(text);
            }
            if (mButton != null) {
                mButton.setText(remain == 0 ? R.string.finished : R.string.cancel);
            }
        });
        return true;
    }
}
