package com.sunzee.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunzee.R;
import com.sunzee.model.domain.Name;
import com.sunzee.utils.SharedPreferencesUtils;
import com.sunzee.utils.ToastUtil;
import com.sunzee.utils.UiUtil;

/**
 * 开关机对话框 dialog
 */
public class ShutdownDialog extends Dialog implements View.OnClickListener {

    public static final int OK = 2;
    public static final int NO = 3;
    private final Context mContext;
    private TextView tvTitle;
    private TextView tvContent;
    private Button btnConfirm;
    private Button btnCancel;
    private ImageView IvQrCode;

    public void setListener(DialogClickListener listener) {
        this.listener = listener;
    }

    private DialogClickListener listener;


    public ShutdownDialog(@NonNull Context context) {
        super(context, R.style.DialogBgD);//加载样式
        mContext = context;
        setContentView(R.layout.dialog_normal);
        initView();
    }

    protected void initView() {
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        btnConfirm = findViewById(R.id.btn_confirm);
        btnCancel = findViewById(R.id.btn_cancel);
        IvQrCode = findViewById(R.id.iv_qr_code);
        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        int param = (int) SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE, -1);
        if (param != 1) {
            btnCancel.setLines(1);
            btnConfirm.setLines(1);
        }
    }

    /**
     * set button text
     *
     * @param strIdTitle title text
     * @param strId      shutdown text
     * @param strid1     open text
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setBtnText(int strIdTitle, int content, int strId, int strid1) {
        tvTitle.setText(strIdTitle);
        tvContent.setText(content);
        btnConfirm.setText(strId);
        btnCancel.setText(strid1);
        btnCancel.setTextColor(UiUtil.getColorRes(R.color.white));
    }

    /**
     * get the text of button
     */
    public String getBtnTextConfirm() {

        return btnConfirm.getText().toString().trim();
    }

    /**
     * get the text of buttonCancel
     */
    public String getBtnTextCancel() {

        return btnCancel.getText().toString().trim();
    }


    /**
     * 设置文字
     *
     * @param text
     */
    public void setContent(String text) {
        tvContent.setText(text);
    }


    @Override
    public void onClick(View v) {
        /**
         * 1.如果==0，并且超過30秒
         * ·我們需要重新開始，也就是可以開關機，也就是開
         *
         */
        long openCloseFacility = (long) SharedPreferencesUtils.getParam("open_close_facility", (long) 0);
        long data = System.currentTimeMillis() - openCloseFacility;
        long shengyuData = openCloseFacility - System.currentTimeMillis();
        if (openCloseFacility == 0 | data > 30000) {
            listener.onClickListener(v.getId());
//            new Thread(new CountDownThread()).start();
            SharedPreferencesUtils.setParam("open_close_facility", System.currentTimeMillis());
        } else {
            ToastUtil.showToast(String.format(mContext.getString(R.string.no_continuous_oepn_close_chance), (30 - ((Math.abs(shengyuData) % (1000 * 60)) / 1000))));
        }
        return;

    }

    public interface DialogClickListener {

        void onClickListener(int type);

    }
}
