package com.mq.zhicang.tianjin;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;

import com.mq.mylibrary.CardData;
import com.mq.mylibrary.base.BaseNfcActivity;
import com.mq.mylibrary.tool.ETTool;
import com.mq.mylibrary.tool.LogTool;
import com.mq.mylibrary.tool.MusicTool;
import com.mq.mylibrary.tool.StringTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.zhicang.App;
import com.mq.zhicang.R;
import com.mq.zhicang.db.ZhiCangDao;
import com.mq.zhicang.db.ZhiCangEntity;

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

/**
 * 包名: com.mq.zhicang.tianjin
 * 类名: StopReasonsActivity
 * 作用描述: 终止入库填写页
 * 创建人: mQ
 * 创建时间: 2020/7/10 15:07
 * 更新人:
 * 更新时间: 2020/7/10 15:07
 * 更新说明:
 * 版本号: 1.0
 */
public class StopReasonsActivity extends BaseNfcActivity {


    @BindView(R.id.et_remark)
    EditText mEtRemark;

    private ZhiCangEntity mEntity;
    private boolean saving;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stop_reasons);
        ButterKnife.bind(this);
        //取出读卡数据
        mEntity = (ZhiCangEntity) getIntent().getSerializableExtra("bean");
    }

    @SuppressLint("StaticFieldLeak")
    @OnClick({R.id.bar_back, R.id.btn_stop, R.id.btn_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.btn_stop:
                if (StringTool.isEmpty(ETTool.getData(mEtRemark))) {
                    ToastTool.showLong("请输入终止原因");
                    return;
                }

                if (saving) {
                    return;
                }
                saving = true;
                showPd();
                new AsyncTask<Void, Void, CardData>() {
                    @Override
                    protected CardData doInBackground(Void... voids) {
                        return save();
                    }

                    @Override
                    protected void onPostExecute(CardData cardData) {
                        super.onPostExecute(cardData);
                        if (cardData.state.equals("0")) {
                            ToastTool.showLong("保存数据及写卡成功！");
                            MusicTool.load(getContext());
                            new AlertDialog.Builder(getContext())
                                    .setCancelable(false)
                                    .setMessage("保存数据及写卡成功！")
                                    .setPositiveButton("完成", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            setResult(10010);
                                            finish();
                                        }
                                    }).create().show();
                        } else {
                            ToastTool.showLong(cardData.msg);
                        }
                        saving = false;
                        cancelPd();
                    }
                }.execute();
                break;
            case R.id.btn_clear:
                mEtRemark.setText("");
                break;
        }
    }

    private CardData save() {
        CardData cardData = new CardData();
        cardData.state = "4";
        cardData.msg = "保存或写卡失败";

        LogTool.i("值仓---开始写卡保存");

        mEntity.stop_reasons = ETTool.getData(mEtRemark);

        String cardInfo = mEntity.getWriteDataTianjin();
        LogTool.i("值仓---写卡：" + cardInfo);
        try {
            //写卡
            String s = writeTag(cardInfo);
            if (!s.equals("")) {
                cardData.msg = s;
                if (!App.isSkipCard)//跳过卡片时不返回错误信息
                    return cardData;
            }
            //保存数据
            LogTool.i("值仓---写入数据库");
            long id = ZhiCangDao.get().insert(App.getDBHelper(), mEntity);
            if (id > 0) {
                cardData.state = "0";
                cardData.msg = "写卡成功";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return cardData;
    }
}
