// 协议栈模块
// 秒寻科技
// zt 2024-09-14

package mcp

import (
	"encoding/binary"
	"errors"
	"fmt"
	"ipsomc/module/bparam/bparammodel"
	"ipsomc/module/oam"
	"ipsomc/module/ps/psmodel"
	"ipsomc/public"
)

type Mcp struct {
	oamDeviceApi  oam.OamDevice
	oamNetworkApi oam.OamNetwork
	oamGainApi    oam.OamGain
	oamSyncApi    oam.OamSync
	oamBtsApi     oam.OamBts
	oamInstallApi oam.OamInstall
	rtcApi        oam.OamRtc
}

// 将响应发送到http模块
func (obj *Mcp) McpSendRespDataToHttp(stHeadModel *psmodel.PS_HEAD_T, dataModel *bparammodel.BtsBaseParam, byModuleId uint8, byMcpCmd uint8) {
	//生成http请求id
	requestIdStr := fmt.Sprintf("%08X", int(stHeadModel.StVpHead.ProjectID)) + "-" + fmt.Sprintf("%08X", stHeadModel.StVpHead.DeviceID) + "-" + fmt.Sprintf("%d", byMcpCmd)

	//从map中查找通道
	chanRespData, exist := public.Gpub_mapChan[requestIdStr]
	if exist { //通道存在
		chanRespData <- dataModel //发送数据
	}
}

// MCP-A查询响应，要将查询结果反馈给页面模型
// dataList:MCP层数据队列
// wLen:数据队列长度
func (obj *Mcp) McpAUlRespQuery(stHeadModel *psmodel.PS_HEAD_T, dataList []byte, wLen int) error {
	btsBaseParamModel := new(bparammodel.BtsBaseParam)
	var wCurrentLen uint16 = 0
	var wLtvLen uint16 = 0
	var wCommand uint16 = 0
	ltvData := make([]byte, psmodel.PS_MCP_A_DATA_LEN)
	var byModuleId uint8 = 0

	//遍历数据队列
	for {
		if wCurrentLen >= uint16(wLen) {
			break
		}

		//组织一个ltv数据
		wLtvLen = uint16(binary.LittleEndian.Uint16(dataList[wCurrentLen : wCurrentLen+2]))    //长度
		wCommand = uint16(binary.LittleEndian.Uint16(dataList[wCurrentLen+2 : wCurrentLen+4])) //命令
		copy(ltvData, dataList[wCurrentLen+4:wCurrentLen+wLtvLen])                             //数据

		//根据监控命令处理
		if wCommand <= 0x00FF { // 设备信息
			obj.oamDeviceApi.OamDeviceQueryResp(btsBaseParamModel, wCommand, ltvData, wLtvLen-4)
			if byModuleId != public.BTS_MODULE_DEVICE {
				byModuleId = public.BTS_MODULE_DEVICE
			}
		} else if wCommand >= 0x0100 && wCommand <= 0x01FF { // 网管参数
			obj.oamNetworkApi.OamNetrowkQueryResp(btsBaseParamModel, wCommand, ltvData, wLtvLen-4)
			if byModuleId != public.BTS_MODULE_NETWORK {
				byModuleId = public.BTS_MODULE_NETWORK
			}
		} else if wCommand >= 0x0200 && wCommand <= 0x02FF { // 上报参数
			if byModuleId != public.BTS_MCP_REPORT {
				byModuleId = public.BTS_MCP_REPORT
			}
		} else if wCommand >= 0x0300 && wCommand <= 0x03FF { // 增益参数
			obj.oamGainApi.OamGainQueryResp(btsBaseParamModel, wCommand, ltvData, wLtvLen-4)
			if byModuleId != public.BTS_MODULE_GAIN {
				byModuleId = public.BTS_MODULE_GAIN
			}
		} else if wCommand >= 0x0400 && wCommand <= 0x04FF { // 安装参数
			obj.oamInstallApi.OamInstallQueryResp(btsBaseParamModel, wCommand, ltvData, wLtvLen-4)
			if byModuleId != public.BTS_MODULE_INSTALL {
				byModuleId = public.BTS_MODULE_INSTALL
			}
		} else if wCommand >= 0x0500 && wCommand <= 0x05FF { // 同步参数
			obj.oamSyncApi.OamSyncQueryResp(btsBaseParamModel, wCommand, ltvData, wLtvLen-4)
			if byModuleId != public.BTS_MODULE_SYNC {
				byModuleId = public.BTS_MODULE_SYNC
			}
		} else if wCommand >= 0x0600 && wCommand <= 0x06FF { // 基站参数
			obj.oamBtsApi.OamBtsQueryResp(btsBaseParamModel, wCommand, ltvData, wLtvLen-4)
			if byModuleId != public.BTS_MODULE_BTS {
				byModuleId = public.BTS_MODULE_BTS
			}
		} else if wCommand >= 0x0700 && wCommand <= 0x07FF { // RTC参数
			obj.rtcApi.OamRtcQueryResp(btsBaseParamModel, wCommand, ltvData, wLtvLen-4)
			if byModuleId != public.BTS_MODULE_RTC {
				byModuleId = public.BTS_MODULE_RTC
			}
		} else {
			return errors.New("监控命令错误")
		}

		//数据后移
		wCurrentLen += wLtvLen
	}

	//把数据发送至基站基本参数模块
	btsBaseParamModel.ProjectID = stHeadModel.StVpHead.ProjectID
	btsBaseParamModel.BtsID = stHeadModel.StVpHead.DeviceID
	obj.McpSendRespDataToHttp(stHeadModel, btsBaseParamModel, byModuleId, psmodel.MCP_A_QUERY)

	return nil
}

// MCP-A配置响应，要将查询结果反馈给页面模型
// dataList:MCP层数据队列
// wLen:数据队列长度
func (obj *Mcp) McpAUlRespSet(stHeadModel *psmodel.PS_HEAD_T, dataList []byte, wLen int) error {
	btsBaseParamModel := new(bparammodel.BtsBaseParam)
	var wCurrentLen uint16 = 0
	var wLtvLen uint16 = 0
	var wCommand uint16 = 0
	ltvData := make([]byte, psmodel.PS_MCP_A_DATA_LEN)
	var byModuleId uint8 = 0

	//遍历数据队列
	for {
		if wCurrentLen >= uint16(wLen) {
			break
		}

		//组织一个ltv数据
		wLtvLen = uint16(binary.LittleEndian.Uint16(dataList[wCurrentLen : wCurrentLen+2]))    //长度
		wCommand = uint16(binary.LittleEndian.Uint16(dataList[wCurrentLen+2 : wCurrentLen+4])) //命令
		copy(ltvData, dataList[wCurrentLen+4:wCurrentLen+wLtvLen])                             //数据

		//根据监控命令处理
		if wCommand <= 0x00FF { // 设备信息
			if byModuleId != public.BTS_MODULE_DEVICE {
				byModuleId = public.BTS_MODULE_DEVICE
			}
		} else if wCommand >= 0x0100 && wCommand <= 0x01FF { // 网管参数
			if byModuleId != public.BTS_MODULE_NETWORK {
				byModuleId = public.BTS_MODULE_NETWORK
			}
		} else if wCommand >= 0x0200 && wCommand <= 0x02FF { // 上报参数
			if byModuleId != public.BTS_MCP_REPORT {
				byModuleId = public.BTS_MCP_REPORT
			}
		} else if wCommand >= 0x0300 && wCommand <= 0x03FF { // 增益参数
			if byModuleId != public.BTS_MODULE_GAIN {
				byModuleId = public.BTS_MODULE_GAIN
			}
			if wCommand == 0x03FF { //保存参数
				btsBaseParamModel.SaveParam = ltvData[0] //保存参数结果
			}
		} else if wCommand >= 0x0400 && wCommand <= 0x04FF { // 安装参数
			if byModuleId != public.BTS_MODULE_INSTALL {
				byModuleId = public.BTS_MODULE_INSTALL
			}
		} else if wCommand >= 0x0500 && wCommand <= 0x05FF { // 同步参数
			if byModuleId != public.BTS_MODULE_SYNC {
				byModuleId = public.BTS_MODULE_SYNC
			}
		} else if wCommand >= 0x0600 && wCommand <= 0x06FF { // 基站参数
			if byModuleId != public.BTS_MODULE_BTS {
				byModuleId = public.BTS_MODULE_BTS
			}
		} else if wCommand >= 0x0700 && wCommand <= 0x07FF { // RTC参数
			if byModuleId != public.BTS_MODULE_RTC {
				byModuleId = public.BTS_MODULE_RTC
			}
		} else {
			return errors.New("监控命令错误")
		}

		//数据后移
		wCurrentLen += wLtvLen
	}

	//把数据发送至基站基本参数模块
	btsBaseParamModel.ProjectID = stHeadModel.StVpHead.ProjectID
	btsBaseParamModel.BtsID = stHeadModel.StVpHead.DeviceID
	obj.McpSendRespDataToHttp(stHeadModel, btsBaseParamModel, byModuleId, psmodel.MCP_A_CONFIGURE)

	return nil
}
