package com.sunzee.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.TextViewCompat;
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.UiUtil;

/**
 * 通用对话框 dialog[检查更新]
 */
public class NormalDialog extends Dialog implements View.OnClickListener {

    public static final int OK = 2;
    public static final int NO = 3;

    private TextView tvTitle;
    private TextView tvContent;
    private Button btnConfirm;
    private Button btnCancel;
    private ImageView IvQrCode;
    /**
     * 中间进入进出动画（默认动画）
     */
    public static final int ANIM_CENTER_IN_OUT = 0;

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

    private DialogClickListener listener;


    public NormalDialog(Context context) {
        super(context, R.style.DialogBgD);//加载样式
        setContentView(R.layout.dialog_normal);
        initView();
        this.setCanceledOnTouchOutside(false);
    }

    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);

    }

    /**
     * 设置文本
     *
     * @param title   标题
     * @param content 内容
     * @param btnText 按钮文字
     */
    public void setContent(int title, int content, int btnText) {

        tvTitle.setText(title);
        tvContent.setText(content);
        btnConfirm.setText(btnText);
    }


    public void setContent(int title, int content) {
        tvTitle.setText(title);
        tvContent.setText(content);
    }

    /**
     * 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.setBackground(UiUtil.getDrawableRes(R.drawable.shape_btn_add));
        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 titleId     标题
     * @param version     版本名
     * @param isUpdate    是否要更新
     * @param versionInfo 更新内容
     */
    public void setUpdateContent(int titleId, String version, boolean isUpdate, String versionInfo) {
        tvTitle.setText(titleId);
        if (isUpdate) {
            //新版本 更新

            tvContent.setText(UiUtil.getStringRes(R.string.have_update_content1) + version + "\n" + versionInfo);
            btnConfirm.setText(R.string.update_now);
        } else {
            //不是 不更新
            tvContent.setText(R.string.no_update);
            btnConfirm.setText(R.string.confirm);
        }

    }

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

    /**
     * 下载进度
     */
    public void setContent() {
        tvContent.setText(UiUtil.getStringRes(R.string.download_now));
    }

    public void setContent(int size) {
        tvContent.setText(UiUtil.getStringRes(R.string.download_now) + size + "%");
    }

    public void setBtnEgState() {
        int param = (int) SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE, -1);
        if (param == 2) {
            btnCancel.setLines(1);
            TextViewCompat.setAutoSizeTextTypeWithDefaults(btnCancel, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
            btnConfirm.setLines(1);
            TextViewCompat.setAutoSizeTextTypeWithDefaults(btnConfirm, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
        }
        listener.onClickListener(v.getId());

    }

    public void showCardImg() {
        IvQrCode.setVisibility(View.VISIBLE);
    }

    public interface DialogClickListener {

        void onClickListener(int type);

    }
}
