package com.secspace.lib.common.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Point;
import android.support.v7.app.AlertDialog;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.secspace.lib.common.R;
import com.secspace.lib.common.views.DotTrailingProgress;

/**
 * Created by bxy on 2018/10/22.
 */

public class SecSpaceUpdateDialog implements DialogInterface.OnDismissListener {
    private Activity mAct;
    private AlertDialog mDialog;
    private View mView;
    private DotTrailingProgress mDotTrailingProgress;
    private TextView mTipTv, mCompleteTv, mUnCompletedTv;
    private LinearLayout mCheckLl, mIngLl;
    private int mCurrentProgress = 0;

    public SecSpaceUpdateDialog(Activity activity) {
        this.mAct = activity;
    }

    private void initView() {
        mView = mAct.getLayoutInflater().inflate(R.layout.common_dialog_secspace_update, null);
        mDotTrailingProgress = mView.findViewById(R.id.dot_rotate_progress);
        mTipTv = mView.findViewById(R.id.tv_dialog_tip);
        mCompleteTv = mView.findViewById(R.id.tv_dialog_complete_tip);
        mUnCompletedTv = mView.findViewById(R.id.tv_dialog_uncompleted_tip);
        mCheckLl = mView.findViewById(R.id.ll_dialog_check);
        mIngLl = mView.findViewById(R.id.ll_dialog_ing);
    }

    public SecSpaceUpdateDialog show(int current, int all, boolean cancelable) {
        if (mDialog == null) {
            initView();
            AlertDialog.Builder builder = new AlertDialog.Builder(mAct);

            builder.setCancelable(cancelable);
            builder.setOnDismissListener(this);
            builder.setView(mView);
            mDialog = builder.create();
            Window window = mDialog.getWindow();
            window.setBackgroundDrawableResource(R.drawable.common_dialog_system_update_bg);
        }
        if (!mDialog.isShowing()) {
            mDialog.show();
            updateText(current, all);
//            setDialogWindow();
            mDotTrailingProgress.startDotAnimator();

        }
        return this;
    }

    public SecSpaceUpdateDialog updateText(int current, int all) {
        if (mDialog != null) {

            if (all == 0) {
                mCheckLl.setVisibility(View.VISIBLE);
                mIngLl.setVisibility(View.GONE);
                return this;
            }

            mCheckLl.setVisibility(View.GONE);
            mIngLl.setVisibility(View.VISIBLE);

            mCurrentProgress = current * 100 / all;
            mDotTrailingProgress.setProgress(mCurrentProgress);
            String complete = String.format(mAct.getString(R.string.common_exe_secspace_update_complete_tip), current);
            mCompleteTv.setText(complete);
            String unCompleted = String.format(mAct.getString(R.string.common_exe_secspace_update_uncompleted_tip), all - current);
            mUnCompletedTv.setText(unCompleted);
        }
        return this;
    }

    private void setDialogWindow() {
        Window window = mDialog.getWindow();
        Display display = mAct.getWindowManager().getDefaultDisplay();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Point point = new Point();
        display.getSize(point);
        layoutParams.width = point.x;
        layoutParams.height = point.y;
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);
    }

    public void dismiss() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mAct = null;
        mView = null;
        mDotTrailingProgress = null;
        mCompleteTv = null;
        mUnCompletedTv = null;
        mDialog = null;
    }
}
