package m9zApi

import (
	"github.com/gogf/gf/v2/errors/gerror"
	"tgk-touch/internal/module/meterFirm"

	"github.com/towgo/towgo/towgo"
	"tgk-touch/internal/global"
)

func Init() {
	if err := scanConfigManager.loadFromFile(); err != nil {
		g.GVA_LOG.Sugar().Warnf("load scan config failed: %v, using default", err)
		if err := scanConfigManager.SetLoopCount(normalizeLoopCount(g.Config().DeviceInfo.LoopCount)); err != nil {
			panic(err)
		}
	}

	towgo.SetFunc("/m9z/fault/list", faultList)
	towgo.SetFunc("/m9z/fault/batchExec", execFailByIds)
	// 获取设备输出状态参数
	towgo.SetFunc("/m9z/getOutStatus", getOutStatus)
	// 获取设备模式
	towgo.SetFunc("/m9z/getDeviceMode", getDeviceMode)
	//	 设置设备模式
	towgo.SetFunc("/m9z/setDeviceMode", setDeviceMode)
	// 获取手动控制信息
	towgo.SetFunc("/m9z/getDeviceManualInfo", getDeviceManualInfo)
	// 设置手动控制
	towgo.SetFunc("/m9z/setDeviceManualInfo", setDeviceManualInfo)
	//	 获取设备时间
	towgo.SetFunc("/m9z/getDeviceTime", getDeviceTime)
	// 设置设备时间
	towgo.SetFunc("/m9z/setDeviceTime", setDeviceTime)
	// 获取设备经纬度信息
	towgo.SetFunc("/m9z/getLgnLatData", getLgnLatData)
	// 设置设备经纬度信息
	towgo.SetFunc("/m9z/setLgnLatData", setLgnLatData)
	// 获取日出日落时间偏差
	towgo.SetFunc("/m9z/getSunRiseSet", getSunRiseSet)
	// 设置日出日落时间偏差
	towgo.SetFunc("/m9z/setSunRiseSet", setSunRiseSet)
	// 获取设备的状态
	towgo.SetFunc("/m9z/getDeviceStatus", getDeviceStatus)
	// 实时监控
	towgo.SetFunc("/m9z/getDeviceStatus2", getDeviceStatus2)
	//	获取设备的配置
	towgo.SetFunc("/m9z/getDeviceConfig", getDeviceConfig)
	// 设置设备的配置
	towgo.SetFunc("/m9z/setDeviceConfig", setDeviceConfig)
	// 获取日出日落时间
	towgo.SetFunc("/m9z/getDeviceSunRiseTime", getDeviceSunRiseTime)
	// 保存配置参数
	towgo.SetFunc("/m9z/setSave", setSave)
	// 重启设备
	towgo.SetFunc("/m9z/setRestart", setRestart)
	// 设置经纬度和日出日落时间差
	towgo.SetFunc("/m9z/setDeviceLgnLatAndSunRsTime", setDeviceLgnLatAndSunRsTime)
	// 读取开始任务 (优化指令 CMD=0x13)
	towgo.SetFunc("/m9z/getStartTask", getStartTask)
	// 读取中间任务 (优化指令 CMD=0x14)
	towgo.SetFunc("/m9z/getMiddleTaskRead", getMiddleTaskRead)
	// 读取结束任务 (优化指令 CMD=0x15)
	towgo.SetFunc("/m9z/getStopTaskRead", getStopTaskRead)
	// 写入开始任务 (优化指令 CMD=0x13)
	towgo.SetFunc("/m9z/setStartTaskWrite", setStartTaskWrite)

	// 写入中间任务 (优化指令 CMD=0x14)
	towgo.SetFunc("/m9z/setMiddleTaskWrite", setMiddleTaskWrite)

	// 写入结束任务 (优化指令 CMD=0x15)
	towgo.SetFunc("/m9z/setStopTaskWrite", setStopTaskWrite)
	towgo.SetFunc("/m9z/getStartTask2", getStartTask2)
	towgo.SetFunc("/m9z/getStopTask2", getStopTask2)
	towgo.SetFunc("/m9z/setStartTask2", setStartTask2)
	towgo.SetFunc("/m9z/setStopTask2", setStopTask2)
	towgo.SetFunc("/m9z/getMiddleTask2", getMiddleTask2)
	towgo.SetFunc("/m9z/setMiddleTask2", setMiddleTask2)

	towgo.SetFunc("/m9z/getSubLoopParameters", getSubLoopParameters)
	towgo.SetFunc("/m9z/getAllSubLoopParameter", getAllSubLoopParameter)
	towgo.SetFunc("/m9z/touchdisplay/getDeviceID", getTouchDeviceId)
	towgo.SetFunc("/m9z/scan/getConfig", getScanConfig)
	towgo.SetFunc("/m9z/scan/setConfig", setScanConfig)

}

func getTouchDeviceId(rpc towgo.JsonRpcConnection) {
	deviceInfo := g.Config().DeviceInfo
	deviceInfo.MeterFirm = meterFirm.Manager().Get()
	deviceInfo.LoopCount = configuredLoopCount()
	rpc.WriteResult(deviceInfo)
}
func faultDelete(rpc towgo.JsonRpcConnection) {
	var p struct {
		Id int `json:"id"`
	}
	rpc.ReadResult(&p)
	if p.Id == 0 {
		panic(gerror.New("id 为空"))

	}

	err := deleteById(p.Id)
	if err != nil {
		panic(err)
	}
	rpc.WriteResult("ok")
}
func faultList(rpc towgo.JsonRpcConnection) {
	data, err := readData()
	if err != nil {
		panic(err)
	}
	rpc.WriteResult(g.NewPageResult(int64(len(data)), data))
}

func execFailByIds(rpc towgo.JsonRpcConnection) {
	var p struct {
		Ids     []int `json:"ids"`
		Operate uint  `json:"operate"` // 0 处理 1删除
	}
	rpc.ReadParams(&p)
	if p.Operate == 0 {

	} else {
		for _, id := range p.Ids {
			err := deleteById(id)
			if err != nil {
				panic(err)
			}
		}
	}
	rpc.WriteResult("ok")

}
