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

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
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.EditText;
import android.widget.TextView;

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

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.user.UserInfoTool;
import com.tofly.ghrq_route.module.user.controller.UserCallBack;
import com.tofly.ghrq_route.module.user.domain.DeviceBean;
import com.tofly.ghrq_route.common.util.app.AppUtil;

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

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

public class DeviceBindBottomDialog extends Dialog {
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.edit_content_hint)
    TextView editContentHint;
    @BindView(R.id.edit_content)
    EditText editContent;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;

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

    private Context mContext;

    private DeviceBean mDeviceInfo;

    private UserInfoTool userInfoTool;

    public DeviceBindBottomDialog(@NonNull Context context) {
        super(context, R.style.AlertDialog);
        mContext = context;
    }

    private OnDeviceBinded onDeviceBinded;

    public void setOnDeviceBindedd(OnDeviceBinded deviceBinded) {
        onDeviceBinded = deviceBinded;
    }

    public interface OnDeviceBinded {
        void onDeviceBinded(boolean isSuccess);
    }

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

        setContentView(R.layout.bottom_dialog_bind_device);
        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(); // 获取屏幕宽、高用
            lp.width = (int) (d.widthPixels * 0.8);// 高度设置为屏幕的0.6
            dialogWindow.setAttributes(lp);
        }

        button1.setText(android.R.string.ok);
        button2.setText(android.R.string.cancel);

        initData();
    }

    private void initData() {
        userInfoTool = UserInfoTool.getInstance();

        String buildName = Build.PRODUCT;
        editContent.setText(buildName);
    }

    @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 (checkValid()){
                    bindDevice();
                }
                break;
            case android.R.id.button2:
                AppUtil.hideSoftInput(this);
                dismiss();
                break;
        }
    }

    private void bindDevice() {
        collectDeviceInfo();

        userInfoTool.bindPdaDevice(mContext, mDeviceInfo, new UserCallBack(){
            @Override
            public void onPdaDeviceBinded(boolean isSuccess, int errorCode, String errMsg) {
                super.onPdaDeviceBinded(isSuccess, errorCode, errMsg);
                if (isSuccess){
                    if (onDeviceBinded != null) {
                        onDeviceBinded.onDeviceBinded(isSuccess);
                    }
                    DeviceBindBottomDialog.this.dismiss();
                }
            }
        });
    }

    private boolean checkValid(){
        String deviceName = editContent.getText().toString();
        if (deviceName.isEmpty()){
            ToastUtil.showToast(mContext, "请输入设备名称");
            editContent.requestFocus();
            return false;
        }

        return true;
    }

    private void collectDeviceInfo(){
        mDeviceInfo = new DeviceBean();

        String imei = BaseApp.getInstance().getDevieImei();
        mDeviceInfo.setImei(imei);

        String padName = editContent.getText().toString();
        mDeviceInfo.setPadname(padName);

        String buildBrand = Build.BRAND;
        mDeviceInfo.setBrand(buildBrand);

        String buildModel = Build.MODEL;
        mDeviceInfo.setModel(buildModel);

        String buildVersion = Build.VERSION.RELEASE;
        mDeviceInfo.setAndroidVersion(buildVersion);

        String appVersion = AppUtil.getLocalVersionName(mContext);
        mDeviceInfo.setAppversion(appVersion);
    }
}
