package com.mq.xiezaixiaoka;

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.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;

import com.mq.mylibrary.CardData;
import com.mq.mylibrary.base.BaseConstant;
import com.mq.mylibrary.base.BaseNfcActivity;
import com.mq.mylibrary.db.team_group.TeamGroupDao;
import com.mq.mylibrary.db.team_group.TeamGroupEntity;
import com.mq.mylibrary.popupwindow.SpinnerOption;
import com.mq.mylibrary.popupwindow.adapter.SelAdapter;
import com.mq.mylibrary.tool.DateTool;
import com.mq.mylibrary.tool.ETTool;
import com.mq.mylibrary.tool.LogTool;
import com.mq.mylibrary.tool.MusicTool;
import com.mq.mylibrary.tool.SPTool;
import com.mq.mylibrary.tool.StringTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.xiezaixiaoka.db.XieZaiXiaoKaDao;
import com.mq.xiezaixiaoka.db.XieZaiXiaoKaEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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

/**
 * 创建人 mQ
 * 创建时间 2019/8/20 0020 16:59
 * 文件名称 XieZaiActivity
 * 说明:  填写卸载信息
 **/
public class XieZaiActivity extends BaseNfcActivity {

    @BindView(R.id.et_platform)
    EditText mEtPlatform;
    @BindView(R.id.et_position)
    EditText mEtPosition;
    @BindView(R.id.et_finish_count)
    EditText mEtFinishCount;
    @BindView(R.id.tv_work_team)
    AutoCompleteTextView mTvWorkTeam;
    @BindView(R.id.tv_work_group)
    AutoCompleteTextView mTvWorkGroup;
    @BindView(R.id.et_entd)
    EditText mEtEntd;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;

    private XieZaiXiaoKaEntity mEntity;

    private List<String> mTeamList = new ArrayList<>();
    private List<String> mGroupList = new ArrayList<>();
    private boolean saving;


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

        //取出读卡数据
        mEntity = (XieZaiXiaoKaEntity) getIntent().getSerializableExtra("bean");
        //显示登录人名称
        String keeper = (String) SPTool.get(BaseConstant.REAL_NAME, "");
        mEtEntd.setText(keeper);
        //显示当前为作业时间
        String currenTime = DateTool.formatDate(DateTool.getCurrenTimeStamp(), DateTool.FULL_DATE);
        mTvStartTime.setText(currenTime);
        mTvEndTime.setText(currenTime);

        getTeamList();
        setAutoTextView(mTvWorkTeam, mTeamList);
        getGroupList();
        setAutoTextView(mTvWorkGroup, mGroupList);
    }

    @SuppressLint("StaticFieldLeak")
    @OnClick({R.id.bar_back, R.id.tv_work_team, R.id.tv_work_group, R.id.tv_start_time,
            R.id.tv_end_time, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                onBackPressed();
                break;
            case R.id.tv_work_team:
                //作业队
//                getTeamList();
//                showListPopupwindow(mTvWorkTeam, mTeamList, mSelectListener);
                mTvWorkTeam.showDropDown();
                break;
            case R.id.tv_work_group:
                //作业组
//                if (StringTool.isEmpty(ETTool.getData(mTvWorkTeam))) {
//                    ToastTool.showLong("请选择作业队");
//                    mTvWorkTeam.setError("请选择作业队");
//                    return;
//                }
//                showListPopupwindow(mTvWorkGroup, mGroupList, mSelectListener);
                mTvWorkGroup.showDropDown();
                break;
            case R.id.tv_start_time:
                //开始时间
                showDateTimeDialog(mTvStartTime, ETTool.getData(mTvStartTime));
                break;
            case R.id.tv_end_time:
                //结束时间
                showDateTimeDialog(mTvEndTime, ETTool.getData(mTvEndTime));
                break;
            case R.id.btn_save:
//                if (StringTool.isEmpty(ETTool.getData(mTvWorkTeam))) {
//                    ToastTool.showLong("请先选择作业队");
//                    mTvWorkTeam.setError("请先选择作业队");
//                    return;
//                }
//                if (StringTool.isEmpty(ETTool.getData(mTvWorkGroup))) {
//                    ToastTool.showLong("请选择作业组");
//                    mTvWorkGroup.setError("请选择作业组");
//                    return;
//                }
                if (StringTool.isEmpty(ETTool.getData(mEtEntd))) {
                    ToastTool.showLong("请输入登记人");
                    mEtEntd.setError("请输入登记人");
                    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) {
                                            finish();
                                        }
                                    }).create().show();
                        } else {
                            ToastTool.showLong(cardData.msg);
                        }
                        saving = false;
                        cancelPd();
                    }
                }.execute();
                break;
        }
    }


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

        LogTool.i("汽车卸载---开始写卡保存");
        mEntity.platform = (ETTool.getData(mEtPlatform));
        mEntity.position = (ETTool.getData(mEtPosition));
        mEntity.finish_count = (ETTool.getData(mEtFinishCount));
        mEntity.work_team = (ETTool.getData(mTvWorkTeam));
        mEntity.work_group = (ETTool.getData(mTvWorkGroup));
        mEntity.entd = (ETTool.getData(mEtEntd));
        mEntity.start_time = (ETTool.getData(mTvStartTime));
        mEntity.end_time = (ETTool.getData(mTvEndTime));

        LogTool.i("汽车卸载---写卡");
        try {
            //写卡
            String s = clearCard();
            if (!s.equals("")) {
                cardData.msg = s;
                if (!App.isSkipCard)//跳过卡片时不返回错误信息
                    return cardData;
            }
            //保存数据
            LogTool.i("汽车卸载---写入数据库");
            long id = XieZaiXiaoKaDao.get().insert(App.getDBHelper(), mEntity);
            if (id > 0) {
                cardData.state = "0";
                cardData.msg = "写卡成功";
            }

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

    public SelAdapter.SelectListener mSelectListener = new SelAdapter.SelectListener() {
        @Override
        public void onSelectListener(View view, Set<SpinnerOption> selectList) {
            getSelPopupwindow(view, selectList);
            SpinnerOption option = selectList.iterator().next();
            switch (view.getId()) {
                case R.id.tv_work_team:
                    //选择作业队 把作业组旧选择置空
                    getGroupList();
                    mTvWorkGroup.setText("");
                    break;
            }
        }
    };

    private void getTeamList() {
        if (mTeamList.size() > 0)
            return;
        mTeamList.clear();
        List<TeamGroupEntity> list = TeamGroupDao.get().getTeamList(App.getDBHelper());
        if (list != null) {
            for (TeamGroupEntity entity : list) {
                mTeamList.add(entity.work_team);
            }
        }
    }

    private void getGroupList() {
        mGroupList.clear();
        List<TeamGroupEntity> list = TeamGroupDao.get()
                .getGroupList(App.getDBHelper());
        if (list != null) {
            for (TeamGroupEntity entity : list) {
                mGroupList.add(entity.work_group);
            }
        }
    }
}
