package com.mq.zhicang;

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 android.widget.Toast;

import com.mq.mylibrary.CardData;
import com.mq.mylibrary.ChannelTool;
import com.mq.mylibrary.base.BaseNfcActivity;
import com.mq.mylibrary.tool.ActivityTool;
import com.mq.mylibrary.tool.LogTool;
import com.mq.mylibrary.tool.MusicTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.zhicang.db.ZhiCangEntity;
import com.mq.zhicang.shanxi.LogShanxiActivity;
import com.mq.zhicang.shanxi.WriteShanxiActivity;
import com.mq.zhicang.tianjin.LogTianjinActivity;
import com.mq.zhicang.tianjin.WriteTianjinActivity;

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

/**
 * 创建人 mQ
 * 创建时间 2019/9/22 0022 15:09
 * 文件名称 ZhicangActivity
 * 说明:  值仓读卡页面
 **/
public class ZhicangActivity extends BaseNfcActivity {

    @BindView(R.id.tv_code)
    TextView mTvCode;
    @BindView(R.id.tv_customer)
    TextView mTvCustomer;
    @BindView(R.id.tv_car)
    TextView mTvCar;
    @BindView(R.id.tv_grain_type)
    TextView mTvGrainType;
    @BindView(R.id.tv_weight)
    TextView mTvWeight;
    @BindView(R.id.tv_house)
    TextView mTvHouse;
    @BindView(R.id.tv_position)
    TextView mTvPosition;
    @BindView(R.id.tv_quality)
    TextView mTvQuality;

    private ZhiCangEntity mZhiCangEntity;

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

    @OnClick({R.id.bar_back, R.id.btn_read, R.id.btn_zhicang, R.id.bar_record})
    public void onViewClicked(View view) {
        Class aClass = null;
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.bar_record:
                //记录、补卡
                switch (App.mChannelName) {
                    case ChannelTool.C_TIAN_JIN:
                        //天津的记录
                        aClass = LogTianjinActivity.class;
                        break;
                    case ChannelTool.C_SHAN_XI:
                        // 山西的记录
                        aClass = LogShanxiActivity.class;
                        break;
                    default:
                        //普通的记录
                        aClass = LogActivity.class;
                        break;
                }
                ActivityTool.startActivity(getContext(), aClass);
                break;
            case R.id.btn_read:
                clearData();
                //读卡
                readCard();
                break;
            case R.id.btn_zhicang:
                //去值仓
                if (mZhiCangEntity == null) {
                    ToastTool.showLong("请先进行读卡操作!");
                    return;
                }
                switch (App.mChannelName) {
                    case ChannelTool.C_TIAN_JIN:
                        //天津的值仓
                        if (mZhiCangEntity.grain == null || mZhiCangEntity.level == null) {
                            ToastTool.showLong("未化验,请先去化验!");
                            return;
                        }
                        aClass = WriteTianjinActivity.class;
                        break;
                    case ChannelTool.C_SHAN_XI:
                        // 山西的值仓
                        aClass = WriteShanxiActivity.class;
                        break;
                    default:
                        //普通的值仓
                        aClass = WriteActivity.class;
                        break;
                }
                Intent intent = new Intent(this, aClass);
                intent.putExtra("bean", mZhiCangEntity);
                startActivity(intent);
                break;
        }
    }

    /**
     * 读卡
     */
    @SuppressLint("StaticFieldLeak")
    private void readCard() {
        if (App.isSkipCard) {//跳过读卡时模拟数据
            //测试 跳过读卡
            setViewData(new CardData(ZhiCangEntity.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) {
                    Toast.makeText(ZhicangActivity.this, "卡片连接失效，请重新放卡", Toast.LENGTH_SHORT).show();
                } else {
                    LogTool.i("onPostExecute: 读卡状态:" + msg.state);
                    if (msg.state.equals("0")) {
                        setViewData(msg);//给控件赋值
                    } else {
                        Toast.makeText(ZhicangActivity.this, msg.msg, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }.execute();
    }

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

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

        //设置控件
        mTvCode.setText(mZhiCangEntity.work_number);
        mTvCustomer.setText(mZhiCangEntity.customer);
        mTvCar.setText(mZhiCangEntity.car);
        mTvGrainType.setText(mZhiCangEntity.grain);
        mTvWeight.setText(mZhiCangEntity.weight);
        mTvHouse.setText(mZhiCangEntity.plan_house);
        mTvPosition.setText(mZhiCangEntity.plan_position);
        mTvQuality.setText(mZhiCangEntity.quality);
        MusicTool.load(getContext());
    }

    @Override
    protected void onResume() {
        super.onResume();
        clearData();
    }

    private void clearData(){
        mTvCode.setText("");
        mTvCustomer.setText("");
        mTvCar.setText("");
        mTvGrainType.setText("");
        mTvWeight.setText("");
        mTvHouse.setText("");
        mTvPosition.setText("");
        mTvQuality.setText("");
    }

}
