package com.tofly.ghrq_route.common.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.DialogTitle;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.checkVersion.domain.CheckReslult;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/12/27.
 */

public class VersionUpdateHintDialog extends Dialog {
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.main_hint_text)
    TextView textMainHint;
    @BindView(R.id.text_version_name)
    TextView textVersionName;
    @BindView(R.id.text_publish_time)
    TextView textPublishTime;
    @BindView(R.id.text_version_desc)
    TextView textVersionDesc;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;

    private final static String TAG = VersionUpdateHintDialog.class.getName();

    private Context mContext;

    private CheckReslult mVersionUpdate;

    private boolean isMustUpdate;

    private View.OnClickListener onConfirm, onCancel;

    public VersionUpdateHintDialog(@NonNull Context context, CheckReslult checkReslult,
                                   View.OnClickListener clickListener, View.OnClickListener clickListener1) {
        super(context, R.style.AlertDialog);
        mContext = context;
        mVersionUpdate = checkReslult;
        onConfirm = clickListener;
        onCancel = clickListener1;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_version_update_hint);
        ButterKnife.bind(this);


        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

            dialogWindow = getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            dialogWindow.setAttributes(lp);
        }
        initData();
    }

    private void initData() {
        button1.setText(R.string.update);
        button2.setText(android.R.string.cancel);

        if (mVersionUpdate != null) {
            textVersionName.setText(mVersionUpdate.getVersion());
            textPublishTime.setText(mVersionUpdate.getCreateTime());
            textVersionDesc.setText(mVersionUpdate.getNotes());

            boolean isMustUpdate = mVersionUpdate.getIsupdate() == 1;
            button2.setText(isMustUpdate ? R.string.exit : R.string.next_time);

            String message = "软件有更新，要下载安装吗？";
            if (isMustUpdate)
                message = "软件更新后才可继续使用，是否现在更新？";
            textMainHint.setText(message);
        }
    }

    @OnClick({android.R.id.button1, android.R.id.button2})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                if (onConfirm != null){
                    onConfirm.onClick(view);
                }
                dismiss();
                break;
            case android.R.id.button2:
                if (onCancel != null){
                    onCancel.onClick(view);
                }
                dismiss();
                break;
        }
    }
}
