package com.za.water_marker

import com.za.base.Const
import com.za.bean.db.water_marker.WaterMarkerItemBean
import com.za.bean.db.water_marker.WaterMarkerTemplateAndItemRef
import com.za.bean.db.water_marker.WaterMarkerTemplateBean
import com.za.common.log.LogUtil
import com.za.net.BaseObserver
import com.za.net.RetrofitHelper
import com.za.room.RoomHelper
import com.za.water_marker.bean.MyWaterMarkerItemBean
import com.za.water_marker.bean.MyWaterMarkerTemplateBean
import com.za.water_marker.bean.WaterMarkerRequestBean
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers

object WaterMarkerTemplateManager {

    fun fetchWaterTemplate(taskCode: String, taskId: Int, userOrderId: Int) {
        val waterMarkerTemplateAndItemRef = RoomHelper.db?.waterMarkerDao()?.query(taskCode)
        if (waterMarkerTemplateAndItemRef == null) {
            getWaterMarkerTemplateFromNet(userOrderId, taskId, taskCode)
        }
    }

    fun getWaterMarkerTemplate(taskCode: String?): WaterMarkerTemplateAndItemRef? {
        var waterMarkerTemplateAndItemRef = RoomHelper.db?.waterMarkerDao()?.query(taskCode ?: "")
        if (waterMarkerTemplateAndItemRef == null) {
            waterMarkerTemplateAndItemRef = RoomHelper.db?.waterMarkerDao()?.query(Const.NormalWaterMarker)
        }
        return waterMarkerTemplateAndItemRef
    }


    //删除所有的水印模版，但是保留通用的模版
    fun deleteAll() {
        val waterMarkerDao = RoomHelper.db?.waterMarkerDao()
        val waterMarkerTemplateAndItemRefList = waterMarkerDao?.queryAll()
        if (waterMarkerTemplateAndItemRefList.isNullOrEmpty()) {
            LogUtil.print("删除本地所有水印", "水印为空或者为1")
            return
        }
        waterMarkerTemplateAndItemRefList.forEach { it ->
            if (!it.waterMarkerTemplateBean?.taskCode.equals(Const.NormalWaterMarker)) {
                waterMarkerDao.deleteWaterTemplate(it.waterMarkerTemplateBean?.taskCode ?: "")
            }

            it.waterMarkerItemBeanList?.forEach {
                if (!it.taskCode.equals(Const.NormalWaterMarker)) {
                    waterMarkerDao.deleteWaterMarkerItem(it.taskCode ?: "")
                }
            }
        }
        LogUtil.print("删除本地所有水印", "水印删除成功")
    }

    //从网络获取默认模板
    private fun getWaterMarkerTemplateFromNet(userOrderId: Int, taskId: Int, taskCode: String) {
        RetrofitHelper.getDefaultService().getWatermarkTemplateConfig(WaterMarkerRequestBean(userOrderId))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BaseObserver<MyWaterMarkerTemplateBean>() {
                override fun doSuccess(it: MyWaterMarkerTemplateBean?) {
                    LogUtil.print("水印模板获取成功 来自网络", "it==" + it.toString())
                    val waterMarkerTemplateBean = WaterMarkerTemplateBean(
                        templateName = it?.templateName, taskId = taskId,
                        updateTime = it?.updateTime, taskCode = taskCode)
                    RoomHelper.db?.waterMarkerDao()?.insertWaterMarkerTemplate(waterMarkerTemplateBean)
                    it?.templateConfigList?.forEach { item ->
                        val temp: WaterMarkerItemBean = getWaterMarkerItemBean(it.id
                            ?: 0, item, taskCode)
                        RoomHelper.db?.waterMarkerDao()?.insertWaterMarkerItem(temp)
                    }


                    //存储通用模版
                    val normalWaterMarkerTemplateBean = RoomHelper.db?.waterMarkerDao()?.query(Const.NormalWaterMarker)
                    if (normalWaterMarkerTemplateBean == null) {
                        saveDefaultTemplate(it)
                    } else {
                        //更新默认模板
                        if (it?.id == 1) {
                            if (normalWaterMarkerTemplateBean.waterMarkerTemplateBean?.id != 1) {
                                deleteDefaultTemplate()
                                saveDefaultTemplate(it)
                                LogUtil.print("通用模板存储成功", "替换为通用模板")
                            } else {
                                if (!it.updateTime.equals(normalWaterMarkerTemplateBean.waterMarkerTemplateBean.updateTime)) {
                                    deleteDefaultTemplate()
                                    saveDefaultTemplate(it)
                                    LogUtil.print("通用水印模板更新成功", "通用水印模板更新成功")
                                }
                            }
                        }
                    }
                }

                override fun doFailure(code: Int, msg: String?) {
                    LogUtil.print("照片水印获取失败", "code=$code msg=$msg")
                }
            })
    }

    //保存默认的模板
    private fun saveDefaultTemplate(myWaterMarkerTemplateBean: MyWaterMarkerTemplateBean?) {
        val normalTemplate = WaterMarkerTemplateBean(updateTime =
        myWaterMarkerTemplateBean?.updateTime, templateName = myWaterMarkerTemplateBean?.templateName, taskCode = Const.NormalWaterMarker)
        RoomHelper.db?.waterMarkerDao()?.insertWaterMarkerTemplate(normalTemplate)
        myWaterMarkerTemplateBean?.templateConfigList?.forEach {
            val temp: WaterMarkerItemBean = getWaterMarkerItemBean(templateId = myWaterMarkerTemplateBean.id
                ?: 0, waterMarkerItemBean = it, taskCode = Const.NormalWaterMarker)
            RoomHelper.db?.waterMarkerDao()?.insertWaterMarkerItem(temp)
        }
    }

    //删除默认的模板
    private fun deleteDefaultTemplate() {
        val waterMarkerDao = RoomHelper.db?.waterMarkerDao()
        val waterMarkerTemplateAndItemRef = waterMarkerDao?.query(Const.NormalWaterMarker)
        if (waterMarkerTemplateAndItemRef != null) {
            waterMarkerDao.deleteWaterTemplate(Const.NormalWaterMarker)
            waterMarkerDao.deleteWaterMarkerItem(Const.NormalWaterMarker)
        }
    }

    private fun getWaterMarkerItemBean(templateId: Int, waterMarkerItemBean: MyWaterMarkerItemBean, taskCode: String): WaterMarkerItemBean {
        var temp = WaterMarkerItemBean(waterTemplateId = templateId, taskCode = taskCode)
        temp = if (waterMarkerItemBean.right == null) {
            temp.copy(right = -1.0)
        } else {
            temp.copy(right = waterMarkerItemBean.right)
        }

        temp = if (waterMarkerItemBean.left == null) {
            temp.copy(left = -1.0)
        } else {
            temp.copy(left = waterMarkerItemBean.left)
        }

        temp = if (waterMarkerItemBean.top == null) {
            temp.copy(top = -1.0)
        } else {
            temp.copy(top = waterMarkerItemBean.top)
        }

        temp = if (waterMarkerItemBean.bottom == null) {
            temp.copy(bottom = -1.0)
        } else {
            temp.copy(bottom = waterMarkerItemBean.bottom)
        }
        temp = temp.copy(content = waterMarkerItemBean.content)
        temp = temp.copy(color = waterMarkerItemBean.color)
        temp = temp.copy(highFont = waterMarkerItemBean.highFont)
        temp = temp.copy(wideFont = waterMarkerItemBean.wideFont)
        return temp
    }
}
