package com.cr.common

import com.cr.map.LayerConfigModel
import com.cr.map.LayerModel
import com.cr.models.IPAndComModel
import com.cr.models.UserModel

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/3 10:59
 * 描述：数据操作类
 */
class DataManager {
    // todo: 2023/4/3 静态方法及属性封装
    companion object {

        /**
         * 获取网络服务连接信息
         * @return IPAndComModel
         */
        fun getNetworkLinkInfo(): IPAndComModel {
            var sql: String = "SELECT * FROM ipcom where CHECKED = '1'"
            var res: List<HashMap<String, String>> = DatabaseAppManager.getInstance().query(sql)
            var map = res[0]
            map.let {
                return IPAndComModel.initByMap(it)
            }
        }

        /**
         * 获取网络连接列表
         * @return List<IPAndComModel>
         */
        fun getNetworkLinks(): List<IPAndComModel> {
            var res = mutableListOf<IPAndComModel>()
            var sql: String = "SELECT * FROM ipcom order by id"
            var qList: List<HashMap<String, String>> = DatabaseAppManager.getInstance().query(sql)
            for (map in qList) {
                var item = IPAndComModel.initByMap(map)
                res.add(item)
            }
            return res
        }

        /**
         * 查询指定的连接是否存在
         * @param queryItem IPAndComModel 连接信息
         * @return Boolean
         */
        fun queryNetworkLink(queryItem: IPAndComModel): Boolean {
            var sql =
                "select * from ipcom where ip='${queryItem.ip}' and com='${queryItem.com}' and server='${queryItem.serverName}'"
            var qList: List<HashMap<String, String>> = DatabaseAppManager.getInstance().query(sql)
            return qList != null && qList.isNotEmpty()
        }

        /**
         * 更新网络连接选择状态
         * @param item IPAndComModel 网络连接
         * @param isChecked Boolean 选中状态
         * @return Boolean
         */
        fun updateNetworkChecked(item: IPAndComModel, isChecked: Boolean): Boolean {
            var where = "ip='${item.ip}' and com='${item.com}' and server='${item.serverName}'"
            var checkedValue = if (isChecked) "1" else "0"
            var values = hashMapOf("checked" to checkedValue)
            return DatabaseAppManager.getInstance().update("ipcom", values, where)
        }

        /**
         * 插入新的网络连接
         * @param item IPAndComModel 网络连接
         * @return Boolean
         */
        fun insertNetworkLink(item: IPAndComModel): Boolean {
            var values = hashMapOf(
                "checked" to "1",
                "ip" to item.ip,
                "com" to item.com,
                "server" to item.serverName
            )
            return DatabaseAppManager.getInstance().insert("ipcom", values)
        }

        /**
         * 取消所有连接的选中状态
         * @return Boolean
         */
        fun cancelNetworkChecked(): Boolean {
            var values = hashMapOf("checked" to "0")
            return DatabaseAppManager.getInstance().update("ipcom", values, "")
        }

        /**
         * 存储账号和密码
         * @param userName String 账号
         * @param password String 密码
         * @return Boolean
         */
        fun saveUser(userName: String, password: String): Boolean {
            // todo: 2023/4/6 由于只保留一条数据 保存前先删除
            deleteUser()
            // todo: 2023/4/6 定义插入数据
            var userMap: HashMap<String, String> = hashMapOf()
            userMap["name"] = userName
            userMap["password"] = password
            userMap["issave"] = "1"
            return DatabaseAppManager.getInstance().insert("user", userMap)
        }

        /**
         * 删除账号和密码
         * @return Boolean
         */
        fun deleteUser(): Boolean {
            return DatabaseAppManager.getInstance().delete("user", "")
        }

        /**
         * 获取存储的账号信息
         * @return UserModel
         */
        fun getUser(): UserModel {
            var user = UserModel()
            var sql = "SELECT * FROM user"
            var qList: List<HashMap<String, String>> = DatabaseAppManager.getInstance().query(sql)
            if (qList != null && qList.isNotEmpty()) {
                user.userId = qList[0]["name"]
                user.userPwd = qList[0]["password"]
            }
            return user
        }

        /**
         * 获取切片图层配置
         * @return MutableList<LayerModel>
         */
        fun getTileLayerConfig(): MutableList<LayerModel> {
            // todo: 2023/4/13 返回的图层
            var layers: MutableList<LayerModel> = mutableListOf()
            var sql = "select * from TileLayerConfig order by id"
            var qList: List<HashMap<String, String>> =
                DatabaseConfigManager.getInstance().query(sql)
            if (qList != null && qList.isNotEmpty()) {
                for (map in qList) {
                    var lyrName = map["lyrname"]
                    var lyrIsVisible = map["isvisible"].equals("1")
                    var lyrUrl = map["lyrurl"]?.replace("/tile/", "")
                    var model: LayerModel = LayerModel(lyrName!!, lyrIsVisible, lyrUrl!!)
                    layers.add(model)
                }
            }
            // todo: 2023/4/13 返回
            return layers
        }

        /**
         * 获取矢量数据配置
         * @return MutableList<LayerConfigModel>
         */
        fun getVectorLayerConfig(): MutableList<LayerConfigModel> {
            // todo: 2023/4/13 定义数据集
            var layerList: MutableList<LayerConfigModel> = mutableListOf()
            var sql = "SELECT * FROM BaseLayerConfig"
            var qList: List<HashMap<String, String>> =
                DatabaseConfigManager.getInstance().query(sql)
            if (qList != null && qList.isNotEmpty()) {
                for (map in qList) {
                    var model = LayerConfigModel(
                        map["name"]!!,
                        map["lyrname"]!!,
                        map["idx"]!!.toInt(),
                        map["isvisible"].equals("1")
                    )
                    layerList.add(model)
                }
            }
            // todo: 2023/4/13 返回数据集合
            return layerList
        }
    }
}