package com.quyunshuo.module.home.service

import android.util.Log

import com.module.pay.service.HomeApiService
import com.quyunshuo.androidbaseframemvvm.base.mvvm.m.BaseRepository
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.constant.Heartbeat
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.constant.SavaAddress
import java.net.URL
import java.net.URLConnection
import javax.inject.Inject

/**
 * 首页M层
 *
 * @author Qu Yunshuo
 * @since 5/25/21 5:42 PM
 */

class MqServiceRepository @Inject constructor() : BaseRepository() {
    private val TAG = "MqServiceRepository"

    @Inject
    lateinit var mApi: HomeApiService


    //广告规则读取
    suspend fun getVideoRule(id: String) = request<String> {
        val lid = id.toLong()
        val params = HashMap<String, Long>()
        params["id"] = lid
        mApi.getVideoRule(params).run {
            emit(this)
        }
    }

    //获取mq中的相关信息
    suspend fun getAddQueue() = request<String> {
        val params = HashMap<String, Any>()
        Log.d(TAG, "Heartbeat.deviceId-->" + Heartbeat.deviceId)

        val machineType: String =
            SpUtils.getString(MMKVName.MACHINE_TYPE, SavaAddress.MACHINE_TYPE)!!
        params["exchange"] = machineType //交换机的名称，机型
        params["queue"] = Heartbeat.deviceId!! //队列的名称，机器的设备编码
        params["routingKey"] = Heartbeat.deviceId!! //队列的key，机器的设备编码
        params["durable"] = true //队列的key，机器的设备编码
        Log.d(TAG, ": mq getAddQueue: $machineType")
        mApi.addQueue(params).run {
            SpUtils.putString(MMKVName.QUEUE_DATA, this)
            emit(this)
        }
    }

    /**
     * 修改通道
     */
    suspend fun getChannel() = request<String> {
        val params = HashMap<String, String>()
        params["clientId"] = Heartbeat.deviceId!! //设备编号
        params["type"] = "2" //type：信道的方式：1：个推，2：Mq ，默认是2
        mApi.updateChannel(params).run {
//            SpUtils.putString(MMKVName.CHANNEL_DATA, this)
            Log.d(TAG, "getChannel发送成功mqservice修改通道$this")
            emit(this)
        }
    }

    suspend fun updateEqeStatus(status: Int) = request<String> {
        var url = URL("http://time.tianqi.com/")
        val uc: URLConnection = url.openConnection() // 生成连接du对象
        uc.connect() // 发出连接
        val ld = uc.date // 取得网站日期时间
        //   val params: MutableMap<String, Any> = java.util.HashMap()
        val params = HashMap<String, Any>()

        params["clientId"] = Heartbeat.deviceId!!
        params["eqeStatus"] = status
        params["netTime"] = ld.toString() + ""
        mApi.updateEqeStatusNew(params).run {
            emit(this)
        }
    }


    /**
     * 修改通道
     */
    suspend fun updateSugarDoStatus(params: Map<String, Any>) = request<String> {

        mApi.updateSugarDoStatus(params).run {
            emit(this)
        }
    }
}