package com.mq.zhicang;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.mq.mylibrary.base.BaseNfcActivity;
import com.mq.mylibrary.popupwindow.SpinnerOption;
import com.mq.mylibrary.popupwindow.adapter.SelAdapter;
import com.mq.mylibrary.tool.ActivityTool;
import com.mq.mylibrary.tool.MusicTool;
import com.mq.mylibrary.tool.StringTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.zhicang.db.ZhiCangDao;
import com.mq.zhicang.db.ZhiCangEntity;

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

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

/**
 * 创建人 mQ
 * 创建时间 2019/8/14 0014 18:40
 * 文件名称 LogActivity
 * 说明:  值仓记录
 **/
public class LogActivity extends BaseNfcActivity {

    @BindView(R.id.tv_car)
    TextView mTvCar;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private LogAdapter mLogAdapter;

    private List<SpinnerOption> mCarList = new ArrayList<>();

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

        mLogAdapter = new LogAdapter(null);
        mLogAdapter.setCallBack(new LogAdapter.CallBack() {
            @Override
            public void onLookListener(ZhiCangEntity entity) {
                //查看
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", entity);
                ActivityTool.startActivity(getContext(), DetailActivity.class, bundle);
            }

            @SuppressLint("StaticFieldLeak")
            @Override
            public void onCardListener(ZhiCangEntity entity) {
                //写卡
                showPd();
                new AsyncTask<Void, Void, String>() {

                    @Override
                    protected String doInBackground(Void... voids) {
                        String writeData = entity.getWriteData();
                        return writeTag(writeData);
                    }

                    @Override
                    protected void onPostExecute(String s) {
                        super.onPostExecute(s);
                        if (StringTool.isEmpty(s)) {
                            ToastTool.showLong("补卡成功");
                            new AlertDialog.Builder(getContext())
                                    .setMessage("补卡成功")
                                    .setPositiveButton("完成", null)
                                    .create().show();
                            MusicTool.load(getContext());
                        } else
                            ToastTool.showLong(s);
                        cancelPd();
                    }
                }.execute();
            }
        });
        mRecyclerView.setAdapter(mLogAdapter);

        List<ZhiCangEntity> carList = ZhiCangDao.get().getCarList(App.getDBHelper());
        for (ZhiCangEntity entity : carList) {
            mCarList.add(new SpinnerOption(entity.car));
        }

        getLogList(null);
    }

    @OnClick({R.id.bar_back, R.id.layout_car})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                onBackPressed();
                break;
            case R.id.layout_car:
                //选择车辆编号
                showListPopupwindow(mTvCar, mCarList, new SelAdapter.SelectListener() {
                    @Override
                    public void onSelectListener(View view, Set<SpinnerOption> selectList) {
                        getSelPopupwindow(view, selectList);
                        SpinnerOption option = selectList.iterator().next();
                        getLogList(option.name);
                    }
                });
                break;
        }
    }

    private void getLogList(String car) {
        List<ZhiCangEntity> list = ZhiCangDao.get().getListByCar(App.getDBHelper(), car);
        mLogAdapter.replaceData(list);
    }
}
