package com.hw.nativeapp.ui.activity;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.SPUtils;
import com.hw.nativeapp.MApplication;
import com.hw.nativeapp.R;
import com.hw.nativeapp.config.Contents;
import com.hw.nativeapp.httpnet.ErrorConsumer;
import com.hw.nativeapp.httpnet.ResponseConsumer;
import com.hw.nativeapp.httpnet.entity.LoginBean;
import com.hw.nativeapp.httpnet.entity.LoginRequestBean;
import com.hw.nativeapp.ui.dialogs.DialogInputDevice;
import com.hw.nativeapp.ui.dialogs.DialogModifyText;
import com.hw.nativeapp.utils.ActivityUtils;
import com.hw.nativeapp.utils.DeviceUtils;
import com.hw.nativeapp.utils.LogUtils;
import com.hw.nativeapp.utils.RxUtil;
import com.hw.nativeapp.utils.StringUtils;
import com.hw.nativeapp.utils.ToastUtils;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class MainActivity extends BaseActivity  {

    private Long exitTemptime = 0L;

    private TextView paper_text;

    private String deviceId = "";

    @BindView(R.id.tx_customerMobile)
    TextView tx_customerMobile;
    @BindView(R.id.main_img)
    ImageView main_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        selfIndex();
    }

    @Override
    void initView() {
        // 获取设备唯一标识
        try {
            deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        }catch (Exception e){
            deviceId = "";
        }
        if(deviceId.isEmpty()){
            DialogInputDevice.Builder builder = new DialogInputDevice.Builder(this);
            builder.setTitle("请输入设备号")
                    .setPositionButton("确定", (dialog, which) -> {
                        if (StringUtils.isEmpty(builder.getNewContent())){
                            ToastUtils.showLongToast(MainActivity.this, "设备号不能为空");
                        } else {
                            deviceId = builder.getNewContent();
                            loginIn();
                            dialog.dismiss();
                        }
                    })
                    .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                    .create()
                    .show();
        } else {
            loginIn();
            Log.e("deviceId","---------------deviceId--------------"+deviceId);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK &&event.getAction() == KeyEvent.ACTION_DOWN){
            if(System.currentTimeMillis() - exitTemptime > 2000) {
                Toast.makeText(this, "请在按一次返回退出", Toast.LENGTH_LONG).show();
                exitTemptime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }else if (keyCode == KeyEvent.KEYCODE_HOME){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    // take_btn 自助取票
    // purchase_btn 自助购票
    @OnClick({R.id.take_btn,R.id.purchase_btn})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.take_btn:
                ActivityUtils.launchActivity(this,TakeActivity.class);
                break;
            case R.id.purchase_btn:
                ActivityUtils.launchActivity(this,TicketPurchaseActivity.class);
                break;
        }
    }

    // 模拟登录
    private void loginIn(){
        LoginRequestBean requestBean = new LoginRequestBean();
        requestBean.deviceNo = deviceId;
        MApplication.getApiService().login( requestBean, System.currentTimeMillis() / 1000)
                .compose(RxUtil.applyObservableAsync())
                .subscribe(new ResponseConsumer<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean data) {
                        //缓存token登录信息
                        SPUtils.getInstance().put(Contents.SPU_TOKEN_KEY, "Bearer " + data.getAccessToken());
                    }

                    @Override
                    public void onFailed(int code, String msg) {

                    }
                }, new ErrorConsumer());
    }

    // 首页数据
    private void selfIndex(){
        MApplication.getApiService().selfIndex(  System.currentTimeMillis() / 1000)
                .compose(RxUtil.applyObservableAsync())
                .subscribe(new ResponseConsumer<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject data) {
                        tx_customerMobile.setText("客服电话：" + data.getString("customerMobile"));
                        JSONArray list = data.getJSONArray("advList");
                        if (list != null && list.size() > 0) {
                            JSONObject jsonObject = list.getJSONObject(0);
                            String sliderImg = jsonObject.getString("sliderImg");
                            Picasso.with(MainActivity.this).load(sliderImg).into(main_img);
                        }
                    }

                    @Override
                    public void onFailed(int code, String msg) {
                    }
                }, new ErrorConsumer());
    }
}