package com.casic.electric.detector.utils

import com.casic.electric.detector.base.BaseApplication
import com.casic.electric.detector.bean.LabelBean
import com.casic.electric.detector.bean.SmallLabelBean
import com.casic.electric.detector.greendao.LabelBeanDao
import com.casic.electric.detector.greendao.SmallLabelBeanDao
import com.pengxh.kt.lite.extensions.toJson
import org.greenrobot.greendao.Property

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

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

    /******************************* Lable *** Start **********************************************/

    fun clearLabels() {
        labelBeanDao.deleteAll()
    }

    //下载服务器表格后存本地
    fun insertLabel(labelBean: LabelBean) {
        labelBeanDao.insert(labelBean)
    }

    //上传服务器之前存本地
    fun insertLabel(labelData: LabelDataClass) {
        val labelBean = LabelBean()
        labelBean.markerObjType = labelData.recordType
        labelBean.markerNumber = labelData.markerObjectId
        labelBean.objectName = labelData.objectName
        labelBean.voltageLevel = labelData.pressLevel
        labelBean.transformerModel = labelData.markerObjectType
        labelBean.holeCount = labelData.holeCount
        labelBean.capacity = labelData.capacity
        labelBean.wellCoverMaterial = labelData.wellMaterial
        labelBean.coverDepth = labelData.coverDepth
        labelBean.transformerSpec = labelData.transformerSpec
        labelBean.inlineName = labelData.inlineName
        labelBean.size = labelData.size
        labelBean.bottomDepth = labelData.bottomDepth
        labelBean.height = labelData.height
        labelBean.crossPipeLine = labelData.crossPipe
        labelBean.inlineCount = labelData.inlineCount
        labelBean.lineNumber = labelData.lineNo
        labelBean.jointCount = labelData.jointCount
        labelBean.outlineCount = labelData.outlineCount
        labelBean.remainingChannels = labelData.leftHoleCount
        labelBean.cabinetType = labelData.cabinetType
        labelBean.tieCable1 = labelData.tieCable1
        labelBean.tieCable2 = labelData.tieCable2
        labelBean.casingMaterial = labelData.bushingMaterial
        labelBean.casingSpec = labelData.bushingSpec
        labelBean.constructTime = labelData.constructTime
        labelBean.inspectionUnit = labelData.operComp
        labelBean.area = labelData.area
        labelBean.road = labelData.road
        labelBean.memo1 = labelData.memo1
        labelBean.memo2 = labelData.memo2
        labelBean.memo3 = labelData.memo3
        labelBean.markerId = labelData.markerId
        labelBean.markerType = labelData.markerType
        labelBean.owner = labelData.owner
        labelBean.markerDepth = labelData.markerDepth
        labelBean.electricTagCount = labelData.eleTagCount
        labelBean.installedTime = labelData.createTime
        labelBean.longitude = labelData.longitude
        labelBean.latitude = labelData.latitude
        labelBean.markerMemo1 = labelData.markerMemo1
        labelBean.markerMemo2 = labelData.markerMemo2
        labelBean.markerMemo3 = labelData.markerMemo3
        labelBean.imagePath = labelData.realPaths.toJson()

        labelBeanDao.insert(labelBean)
    }

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

    fun loadLabelByCondition(selectedItem: String, value: String?): MutableList<LabelBean> {
        when (selectedItem) {
            LocaleConstant.CONDITION_ARRAY[1] -> {
                return queryByProperty(LabelBeanDao.Properties.MarkerNumber, value)
            }
            LocaleConstant.CONDITION_ARRAY[2] -> {
                return queryByProperty(LabelBeanDao.Properties.ObjectName, value)
            }
            LocaleConstant.CONDITION_ARRAY[3] -> {
                return queryByProperty(LabelBeanDao.Properties.Area, value)
            }
            LocaleConstant.CONDITION_ARRAY[4] -> {
                return queryByProperty(LabelBeanDao.Properties.Road, value)
            }
            LocaleConstant.CONDITION_ARRAY[5] -> {
                return queryByProperty(LabelBeanDao.Properties.InspectionUnit, value)
            }
            LocaleConstant.CONDITION_ARRAY[6] -> {
                return queryByProperty(LabelBeanDao.Properties.Owner, value)
            }
            LocaleConstant.CONDITION_ARRAY[8] -> {
                return queryByProperty(LabelBeanDao.Properties.MarkerId, value)
            }
        }
        return ArrayList()
    }

    private fun queryByProperty(property: Property, value: String?): MutableList<LabelBean> {
        return if (value == null) {
            labelBeanDao.queryBuilder().where(property.isNotNull).list()
        } else {
            labelBeanDao.queryBuilder().where(property.eq(value)).list()
        }
    }

    fun loadLabelByDate(startDate: String, endDate: String): MutableList<LabelBean> {
        return labelBeanDao.queryBuilder().where(
            LabelBeanDao.Properties.ConstructTime.gt(startDate),
            LabelBeanDao.Properties.ConstructTime.lt(endDate)
        ).list()
    }

    fun queryLabelById(markerId: String): List<LabelBean> {
        return labelBeanDao.queryBuilder()
            .where(LabelBeanDao.Properties.MarkerId.eq(markerId))
            .list()
    }
    /******************************* Lable *** End **********************************************/

    /******************************* Small Lable *** Start **************************************/

    //下载服务器表格后存本地
    fun insertSmallLabel(bean: SmallLabelBean) {
        smallLabelBeanDao.insert(bean)
    }

    //上传服务器之前存本地
    fun insertSmallLabel(labelData: SmallLabelDataClass) {
        val labelBean = SmallLabelBean()
        labelBean.markerId = labelData.markerId
        labelBean.locationNumber = labelData.locationNo
        labelBean.locationName = labelData.locationName
        labelBean.electricMarkerId = labelData.smallMarkerId
        labelBean.cableName = labelData.cableName
        labelBean.cableNumber = labelData.cableNo
        labelBean.line = labelData.line
        labelBean.voltageLevel = labelData.pressLevel
        labelBean.cableType = labelData.cableType
        labelBean.cableSpec = labelData.cableSpec
        labelBean.cableBrand = labelData.cableBrand
        labelBean.cableStart = labelData.cableStart
        labelBean.cableEnd = labelData.cableEnd
        labelBean.hasJoint = labelData.hasJoint
        labelBean.jointCreator = labelData.jointCreator
        labelBean.imagePath = labelData.realPaths.toJson()
        labelBean.memo1 = labelData.memo1
        labelBean.memo2 = labelData.memo2
        labelBean.memo3 = labelData.memo3

        smallLabelBeanDao.insert(labelBean)
    }

    fun clearSmallLabels() {
        smallLabelBeanDao.deleteAll()
    }

    fun loadSmallLabels(): MutableList<SmallLabelBean> {
        return smallLabelBeanDao.loadAll()
    }

    fun queryElectricMarkerById(electricMarkerId: String): SmallLabelBean {
        return smallLabelBeanDao.queryBuilder()
            .where(SmallLabelBeanDao.Properties.ElectricMarkerId.eq(electricMarkerId))
            .list()
            .first()
    }
    /******************************* Small Lable *** End ****************************************/
}