package com.tofly.ghrq_route.module.sqlite;

import android.content.Context;
import android.database.Cursor;

import com.tofly.data.sql.dao.CheckGPSDataBeanDao;
import com.tofly.data.sql.dao.DeviceInfoBeanDao;
import com.tofly.data.sql.dao.GPSDataBeanDao;
import com.tofly.data.sql.dao.LocationGPSDataBeanDao;
import com.tofly.data.sql.dao.OfflinePathBeanDao;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import org.greenrobot.greendao.AbstractDao;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class DBToExcelTool {

    private static final String TAG = DBToExcelTool.class.getName();

    private static DBToExcelTool dbToExcelTool = null;

    private DBDaoHelper dbDaoHelper;

    private DBToExcelTool(){
        dbDaoHelper = DBDaoHelper.getInstance();
    }

    public static synchronized DBToExcelTool getInstance(){
        if (dbToExcelTool == null){
            dbToExcelTool = new DBToExcelTool();
        }
        return dbToExcelTool;
    }

    public <T> void createExcelFile(Context context, Class<T> entityClass, String userid){
        AbstractDao dao = dbDaoHelper.getTableDao(entityClass);
        if (dao == null)
            return;

        String[] title = dao.getAllColumns();

        String dir = C.dir.temp;
        String rootPath = dir;
        if (userid != null && !userid.isEmpty()){
            rootPath = dir + userid + "/";
        }
        File root = new File(rootPath);

        root.mkdirs();

        String path = root.getAbsolutePath() + "//" + entityClass.getSimpleName()
                + "_" + DateUtils.getYMD() + ".xls";
        File file = new File(path);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        ExcelUtil.initExcel(file.getPath(), title, null);
        ExcelUtil.writeObjListToExcel(getBillData(dao, userid), path, context);
    }

    private Cursor getCursorQuery(AbstractDao dao, String userid){
        Cursor cursor = null;
        if (dao instanceof GPSDataBeanDao){
            GPSDataBeanDao gpsDataBeanDao = (GPSDataBeanDao) dao;
            cursor = gpsDataBeanDao.queryBuilder().buildCursor().query();
        }else if (dao instanceof CheckGPSDataBeanDao){
            CheckGPSDataBeanDao checkGPSDataBeanDao = (CheckGPSDataBeanDao) dao;
            cursor = checkGPSDataBeanDao.queryBuilder().buildCursor().query();
        }else if (dao instanceof DeviceInfoBeanDao){
            DeviceInfoBeanDao deviceInfoBeanDao = (DeviceInfoBeanDao) dao;
            cursor = deviceInfoBeanDao.queryBuilder().buildCursor().query();
        }else if (dao instanceof OfflinePathBeanDao){
            OfflinePathBeanDao offlinePathBeanDao = (OfflinePathBeanDao) dao;
            cursor = offlinePathBeanDao.queryBuilder().where(
                    OfflinePathBeanDao.Properties.Userid.eq(userid)
            ).buildCursor().query();
        }
        return cursor;
    }

    private ArrayList<ArrayList<String>> getBillData(AbstractDao dao, String userid) {
        /*  Cursor cursor = dao.queryRaw(" where T.\"USERID\"=? ", new String[]{String.valueOf(userid)}).;*/
        ArrayList<ArrayList<String>>bill2List = new ArrayList<>();
        Cursor cursor = getCursorQuery(dao, userid);
        if (cursor == null)
            return bill2List;

        while (cursor.moveToNext()) {
            ArrayList<String> beanList=new ArrayList<String>();
            for (int i = 0; i < cursor.getColumnCount(); i++){
                beanList.add(cursor.getString(i));
            }
            bill2List.add(beanList);
        }
        cursor.close();
        return bill2List;
    }

    //创建Excel文件
    public <T> String createExcelFile(Context context , Class<T> entityClass, String tableName, String userid, String sTime, String eTime, ArrayList<ArrayList<String>> data){
        AbstractDao dao = dbDaoHelper.getTableDao(entityClass);
        if (dao == null)
            return null;

        String[] title = dao.getAllColumns();

        String dir = C.dir.log;
        String rootPath = dir;
        if (userid != null && !userid.isEmpty()){
            rootPath = dir + userid + "/";
        }
        File root = new File(rootPath);

        root.mkdirs();

        String path = root.getAbsolutePath() + "//" + tableName
                + "_" + sTime + "_" + eTime + ".xls";
       /* File file = new File(path);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        ExcelUtil.initExcel(path, title, tableName);
        ExcelUtil.writeObjListToExcel(data, path, context);

        return path;
    }

    private Cursor getCursorQuery(AbstractDao dao, String userid, String sTime,String eTime){
        Cursor cursor = null;
        if (dao instanceof GPSDataBeanDao){
            GPSDataBeanDao gpsDataBeanDao = (GPSDataBeanDao) dao;
            cursor = gpsDataBeanDao.queryBuilder().where(
                    GPSDataBeanDao.Properties.Check_time.between(sTime, eTime)
            ).buildCursor().query();
        }else if (dao instanceof LocationGPSDataBeanDao){
            LocationGPSDataBeanDao locationGPSDataBeanDao = (LocationGPSDataBeanDao) dao;
            cursor = locationGPSDataBeanDao.queryBuilder().where(
                    LocationGPSDataBeanDao.Properties.Date.between(sTime, eTime)
            ).orderAsc(LocationGPSDataBeanDao.Properties.Date,
                    LocationGPSDataBeanDao.Properties.Gps_time).buildCursor().query();
        }else if (dao instanceof DeviceInfoBeanDao){
            DeviceInfoBeanDao deviceInfoBeanDao = (DeviceInfoBeanDao) dao;
            cursor = deviceInfoBeanDao.queryBuilder().where(
                    DeviceInfoBeanDao.Properties.Check_time.between(sTime, eTime)
            ).buildCursor().query();
        }else if (dao instanceof OfflinePathBeanDao){
            OfflinePathBeanDao offlinePathBeanDao = (OfflinePathBeanDao) dao;
            cursor = offlinePathBeanDao.queryBuilder().where(
                    OfflinePathBeanDao.Properties.Userid.eq(userid),
                    OfflinePathBeanDao.Properties.Child_begin_date.lt(eTime),
                    OfflinePathBeanDao.Properties.Child_end_date.ge(sTime)
            ).orderAsc(OfflinePathBeanDao.Properties.Plan_id,
                    OfflinePathBeanDao.Properties.Child_begin_date).buildCursor().query();
        }
        return cursor;
    }

    public  <T> ArrayList<ArrayList<String>> getBillData(Class<T> entityClass, String userid, String sTime,String eTime){
        AbstractDao dao = dbDaoHelper.getTableDao(entityClass);
        if (dao == null)
            return null;
        return getBillData(dao, userid, sTime, eTime);
    }

    public <T> String createExcelFile(Context context , Class<T> entityClass, String tableName, String userid, String sTime, String eTime){
        ArrayList<ArrayList<String>> data = getBillData(entityClass, userid, sTime, eTime);
        return createExcelFile(context, entityClass, tableName, userid, sTime, eTime, data);
    }

    private ArrayList<ArrayList<String>> getBillData(AbstractDao dao, String userid, String sTime,String eTime) {
        ArrayList<ArrayList<String>>bill2List = new ArrayList<>();
        Cursor cursor = getCursorQuery(dao, userid, sTime, eTime);
        if (cursor == null)
            return bill2List;

        while (cursor.moveToNext()) {
            ArrayList<String> beanList=new ArrayList<String>();
            for (int i = 0; i < cursor.getColumnCount(); i++){
                beanList.add(cursor.getString(i));
            }
            bill2List.add(beanList);
        }
        cursor.close();
        return bill2List;
    }
}
