package com.casic.detector.utils

import com.casic.detector.base.BaseApplication
import com.casic.detector.bean.LabelBean
import com.casic.detector.bean.TaskBean
import com.casic.detector.greendao.LabelBeanDao
import com.casic.detector.greendao.TaskBeanDao

class DataBaseManager private constructor() {
    companion object {
        //Kotlin委托模式双重锁单例
        val get by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            DataBaseManager()
        }
    }

    private var labelBeanDao = BaseApplication.get().getDaoSession().labelBeanDao
    private var taskBeanDao = BaseApplication.get().getDaoSession().taskBeanDao

    fun clearTaskLabels() {
        labelBeanDao.deleteAll()
    }

    fun insertTaskLabel(bean: LabelBean) {
        labelBeanDao.insert(bean)
    }

    fun loadTaskLabels(): MutableList<LabelBean> {
        return labelBeanDao.loadAll()
    }

    fun loadLabelByCondition(selectedItem: String, value: String?): MutableList<LabelBean> {
        val property = when (selectedItem) {
            LocaleConstant.SPINNER_ARRAY[0] -> LabelBeanDao.Properties.IdentifierId
            LocaleConstant.SPINNER_ARRAY[1] -> LabelBeanDao.Properties.Area
            LocaleConstant.SPINNER_ARRAY[2] -> LabelBeanDao.Properties.Line
            LocaleConstant.SPINNER_ARRAY[3] -> LabelBeanDao.Properties.Road
            LocaleConstant.SPINNER_ARRAY[4] -> LabelBeanDao.Properties.Owner
            LocaleConstant.SPINNER_ARRAY[5] -> LabelBeanDao.Properties.Person
            LocaleConstant.SPINNER_ARRAY[6] -> LabelBeanDao.Properties.InstallTime
            else -> LabelBeanDao.Properties.Remark
        }
        return if (value == null) {
            labelBeanDao.queryBuilder().where(property.isNotNull).list()
        } else {
            labelBeanDao.queryBuilder().where(property.eq(value)).list()
        }
    }

    fun queryLabelById(identifierId: String): List<LabelBean> {
        return labelBeanDao.queryBuilder()
            .where(LabelBeanDao.Properties.IdentifierId.eq(identifierId))
            .list()
    }

    fun countTaskById(id: String): Int {
        val count = taskBeanDao.queryBuilder()
            .where(TaskBeanDao.Properties.Id.eq(id))
            .count()
        return count.toInt()
    }

    fun clearTasks() {
        taskBeanDao.deleteAll()
    }

    fun insertTaskInformation(
        scanner: String,
        desc: String,
        taskId: String,
        taskCode: String,
        markerId: String,
        markerRealId: String,
        lng: String,
        lat: String,
        state: String
    ) {
        val task = TaskBean()
        task.scanner = scanner
        task.desc = desc
        task.taskId = taskId
        task.taskCode = taskCode
        task.markerId = markerId
        task.markerRealId = markerRealId
        task.lng = lng
        task.lat = lat
        task.state = state

        taskBeanDao.insert(task)
    }

    fun queryDistinctTask(): List<TaskBean> {
        val result = ArrayList<TaskBean>()
        taskBeanDao.loadAll().forEach {
            result.add(it)
        }
        //根据TaskId去重
        return result.distinctBy { it.taskId }
    }

    fun queryTaskById(taskId: String): List<TaskBean> {
        return taskBeanDao.queryBuilder().where(TaskBeanDao.Properties.TaskId.eq(taskId)).list()
    }

    fun queryTaskMarkerById(markerId: String, state: String): TaskBean? {
        return taskBeanDao.queryBuilder()
            .where(
                TaskBeanDao.Properties.MarkerRealId.eq(markerId),
                TaskBeanDao.Properties.State.eq(state)
            ).list().firstOrNull()
    }

    fun updateTaskLabel(taskBean: TaskBean?) {
        if (taskBean == null) {
            return
        }
        taskBean.state = "已探测"
        taskBeanDao.update(taskBean)
    }

    fun deleteTaskById(taskId: String?) {
        if (taskId.isNullOrBlank()) {
            return
        }
        queryTaskById(taskId).forEach {
            taskBeanDao.delete(it)
        }
    }
}