package com.mq.xiezaixiaoka;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mq.mylibrary.base.BaseNfcActivity;
import com.mq.mylibrary.dialog.StartEndDateDialog;
import com.mq.mylibrary.tool.ActivityTool;
import com.mq.mylibrary.tool.FileTool;
import com.mq.mylibrary.tool.ToastTool;
import com.mq.xiezaixiaoka.db.SyncBean;
import com.mq.xiezaixiaoka.db.XieZaiXiaoKaDao;
import com.mq.xiezaixiaoka.db.XieZaiXiaoKaEntity;

import java.io.File;
import java.util.List;

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

/**
 * 创建人 mQ
 * 创建时间 2019/8/23 0023 9:49
 * 文件名称 LogActivity
 * 说明:
 **/
public class LogActivity extends BaseNfcActivity {

    @BindView(R.id.tv_date)
    TextView mTvDate;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private LogAdapter mLogAdapter;
    private Gson mGson;

    @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(XieZaiXiaoKaEntity entity) {
                //查看
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", entity);
                ActivityTool.startActivity(getContext(), DetailActivity.class, bundle);
            }

        });
        mRecyclerView.setAdapter(mLogAdapter);

        getLogList(null, null);
    }

    @OnClick({R.id.bar_back, R.id.layout_date, R.id.bar_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                onBackPressed();
                break;
            case R.id.layout_date:
                //选择日期
                showStartEndDateDialog(mDateCallBack);
                break;
            case R.id.bar_out:
                //导出
                List<XieZaiXiaoKaEntity> data = mLogAdapter.getData();
                if (data == null || data.size() == 0) {
                    ToastTool.showLong("没有可导出的数据");
                }
                SyncBean syncBean = new SyncBean(data);
                if (mGson == null)
                    mGson = new Gson();
                String json = mGson.toJson(syncBean);
                String fileName = "sync_car_unload.txt";
                boolean result = FileTool.writeTxt(fileName, json);
                if (result) {
                    new AlertDialog.Builder(getContext())
                            .setCancelable(false)
                            .setMessage("导出成功")
                            .setPositiveButton("完成", null)
                            .create().show();
                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    intent.setData(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), fileName)));
                    sendBroadcast(intent);
                } else
                    ToastTool.showLong("导出失败");
                break;
        }
    }

    StartEndDateDialog.DateCallBack mDateCallBack = new StartEndDateDialog.DateCallBack() {
        @Override
        public void onDateResultListener(String startDate, String endDate) {
            dismissStartEndDateDialog();
            mTvDate.setText(startDate + "～" + endDate);
            getLogList(startDate, endDate);
        }
    };

    private void getLogList(String start, String end) {
        List<XieZaiXiaoKaEntity> list = XieZaiXiaoKaDao.get().getList(App.getDBHelper(), start, end);
        mLogAdapter.replaceData(list);
    }
}
