package com.mq.zhongzhuanxiezai;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.mq.mylibrary.CardData;
import com.mq.mylibrary.base.BaseNfcActivity;
import com.mq.mylibrary.db.grain_type.GrainTypeDao;
import com.mq.mylibrary.db.grain_type.GrainTypeEntity;
import com.mq.mylibrary.tool.ActivityTool;
import com.mq.mylibrary.tool.ETTool;
import com.mq.mylibrary.tool.LogTool;
import com.mq.mylibrary.tool.MusicTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.zhongzhuanxiezai.db.XieZaiEntity;

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

/**
 * 创建人 mQ
 * 创建时间 2019/8/20 0020 15:37
 * 文件名称 MainActivity
 * 说明:  发运中转-汽车卸载
 **/
public class MainActivity extends BaseNfcActivity {

    @BindView(R.id.tv_check_number)
    TextView mTvCheckNumber;
    @BindView(R.id.tv_work_number)
    TextView mTvWorkNumber;
    @BindView(R.id.tv_customer)
    TextView mTvCustomer;
    @BindView(R.id.tv_grain)
    TextView mTvGrain;
    @BindView(R.id.tv_car)
    TextView mTvCar;
    @BindView(R.id.tv_weight)
    TextView mTvWeight;
    @BindView(R.id.tv_haulier)
    TextView mTvHaulier;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;

    private XieZaiEntity mEntity;

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

    @OnClick({R.id.bar_back, R.id.bar_record, R.id.btn_read, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                onBackPressed();
                break;
            case R.id.bar_record:
                //记录,补卡
                ActivityTool.startActivity(getContext(), LogActivity.class);
                break;
            case R.id.btn_read:
                //读卡
                readCard();
                break;
            case R.id.btn_next:
                //填写卸载信息
                if (mEntity == null) {
                    ToastTool.showLong("请先进行读卡操作!");
                    return;
                }
                Intent intent = new Intent(this, XieZaiActivity.class);
                intent.putExtra("bean", mEntity);
                startActivity(intent);
                break;
        }
    }

    /**
     * 读卡
     */
    @SuppressLint("StaticFieldLeak")
    private void readCard() {
        if (App.isSkipCard) {//跳过读卡时模拟数据
            setViewData(new CardData(XieZaiEntity.testCardData()));
            return;
        }
        showPd();
        new AsyncTask<Void, Void, CardData>() {

            @Override
            protected CardData doInBackground(Void... voids) {
                return readTag();
            }

            @Override
            protected void onPostExecute(CardData msg) {
                super.onPostExecute(msg);
                cancelPd();
                if (msg == null || msg.state == null) {
                    ToastTool.showLong("卡片连接失效，请重新放卡");
                } else {
                    LogTool.i("onPostExecute: 读卡状态:" + msg.state);
                    if (msg.state.equals("0")) {
                        setViewData(msg);//给控件赋值
                    } else if (msg.state.equals("1")) {
                        ToastTool.showLong("设备不支持NFC！");
                    } else if (msg.state.equals("2")) {
                        ToastTool.showLong("请在系统设置中先启用NFC功能！");
                    } else if (msg.state.equals("3")) {
                        ToastTool.showLong("卡片连接失效，请重新放卡");
                    }
                }
            }
        }.execute();
    }

    //给控件赋值
    private void setViewData(CardData cardData) {
        LogTool.i("解析数据: "
                + cardData.resultData + "\n\n调试数据:" + cardData.debugData + "\n原数据:"
                + cardData.originalData);
        //填充实体类
        XieZaiEntity entity = XieZaiEntity.initData(cardData.resultData);

        if (entity == null) {
            ToastTool.showLong("数据不正确");
            return;
        }
        mEntity = entity;

        //设置控件
        ETTool.setData(mTvCheckNumber, mEntity.check_number);
        ETTool.setData(mTvWorkNumber, mEntity.order_number);
        ETTool.setData(mTvCustomer, mEntity.customer);
        GrainTypeEntity grain = GrainTypeDao.get().getGrain(App.getDBHelper(), mEntity.grain);
        if (grain != null)
            ETTool.setData(mTvGrain, grain.grain_name);
        ETTool.setData(mTvCar, mEntity.car);
        ETTool.setData(mTvWeight, mEntity.weight);
        ETTool.setData(mTvHaulier, mEntity.haulier);
        ETTool.setData(mTvPhone, mEntity.phone);
        MusicTool.load(getContext());
    }
}
