package com.za.room

import android.annotation.SuppressLint
import android.content.Context
import androidx.room.Room.databaseBuilder
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.za.base.Const
import com.za.room.db.GlobalRoom

@SuppressLint("StaticFieldLeak")
object RoomHelper {
    const val VERSION: Int = 41
    private lateinit var mContext: Context
    var db: GlobalRoom? = null

    //ele_work_order 表中新增userOrderId字段
    //ele_car_damage_photo 表中新增userOrderId
    private var MIGRATION_2_3: Migration = object : Migration(2, 3) {
        override fun migrate(db: SupportSQLiteDatabase) {
            db.execSQL("ALTER TABLE ele_work_order" + " ADD COLUMN userOrderId INTEGER NOT NULL DEFAULT 0")
            db.execSQL("ALTER TABLE ele_car_damage_photo" + " ADD COLUMN userOrderId INTEGER NOT NULL DEFAULT 0")
        }
    }

    //增加水印模版
    private var MIGRATION_3_4: Migration = object : Migration(3, 4) {
        override fun migrate(db: SupportSQLiteDatabase) {
            db.execSQL("CREATE TABLE water_template(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " + "templateName TEXT," + "taskCode TEXT," + "templateId INTEGER NOT NULL DEFAULT 0)")
            db.execSQL("CREATE TABLE water_marker_item(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " + "waterTemplateId TEXT," + "taskCode TEXT," + "water_left DECIMAL," + "water_right DECIMAL," + "top DECIMAL," + "bottom DECIMAL," + "content TEXT," + "color TEXT," + "wideFont TEXT," + "highFont TEXT)")
        }
    }

    fun init(context: Context) {
        this.mContext = context
        db = databaseBuilder(mContext, GlobalRoom::class.java, "zd_db")
            .allowMainThreadQueries().addMigrations(MIGRATION_2_3, MIGRATION_3_4)
            .fallbackToDestructiveMigration() //出现异常问题，重新构建表，同时数据会丢失
            .setJournalMode(RoomDatabase.JournalMode.AUTOMATIC).build()
    }

    fun clearAll() {
        db?.orderDao()?.deleteAllOrder()
        db?.photoTemplateDao()?.deleteAllPhotoTemplate()
        db?.changeBatteryDao()?.deleteAll()
        db?.eleWorkOrderDao()?.deleteTable()
        db?.offlineTaskDao()?.deleteAllOfflineTask()
        val temp = db?.waterMarkerDao()?.queryAll()
        temp?.forEach {
            if (it.waterMarkerTemplateBean?.taskCode != Const.NormalWaterMarker) {
                db?.waterMarkerDao()?.deleteWaterMarkerItem(it.waterMarkerTemplateBean?.taskCode
                    ?: "")
                db?.waterMarkerDao()?.deleteWaterTemplate(it.waterMarkerTemplateBean?.taskCode
                    ?: "")
            }
        }
    }

    fun clearOrderFromTaskCode(taskId: Int) {
        db?.orderDao()?.deleteOrderFromTaskId(taskId)
        db?.photoTemplateDao()?.deleteOrderPhotoTemplateFromTaskId(taskId)
        db?.changeBatteryDao()?.deleteFromTaskId(taskId)
        db?.eleWorkOrderDao()?.deleteOrder(taskId)
        val temp = db?.waterMarkerDao()?.queryAll()
        temp?.forEach {
            if (it.waterMarkerTemplateBean?.taskCode != Const.NormalWaterMarker && it.waterMarkerTemplateBean?.taskId == taskId) {
                db?.waterMarkerDao()?.deleteWaterMarkerItem(it.waterMarkerTemplateBean.taskCode
                    ?: "")
                db?.waterMarkerDao()?.deleteWaterTemplate(it.waterMarkerTemplateBean.taskCode ?: "")
            }
        }
    }
}