package plc

import (
	"LAPP_ACURA_MOM_BACKEND/conf"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	"LAPP_ACURA_MOM_BACKEND/infra/logger"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	"LAPP_ACURA_MOM_BACKEND/utils"
	json "github.com/json-iterator/go"
	"github.com/kataras/iris/v12"
)

// ReadDataFromPLC 从PLC读取数据
func ReadDataFromPLC(data baseModel.PLC, log logger.Log, dataType string) (interface{}, error) {
	postData, err := json.Marshal(data)
	if err != nil {
		log.Error("序列化读数据请求数据失败, error:" + err.Error())
		return "", grmi.NewBusinessError("序列化读取数据请求数据失败, error:" + err.Error())
	}
	log.Debug("读取数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend)
	boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData)
	if err != nil {
		log.Error("读取数据调用失败, error:" + err.Error())
		return "", grmi.NewBusinessError("读取数据调用失败, error:" + err.Error())
	}
	log.Debug("读取数据请求响应:" + string(boday))

	var response baseModel.Response
	err = json.Unmarshal(boday, &response)
	if err != nil {
		log.Error("解析读取数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday))
		return "", grmi.NewBusinessError("解析读取数据后台数据失败, error:" + err.Error())
	}
	if response.Code != iris.StatusOK {
		log.Error("读取数据调用后台失败, 后台返回错误, 后台数据:" + string(boday))
		return "", grmi.NewBusinessError("读取数据调用后台失败, 后台返回错误, 错误:" + response.Msg)
	}
	plcData, ok := response.Data.(map[string]interface{})
	if !ok {
		log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
		return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
	}
	successInterface, exist := plcData["IsSuccess"]
	if !exist {
		log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
		return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
	}
	success, ok := successInterface.(bool)
	if !ok {
		log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
		return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
	}
	if !success {
		log.Error("读取数据后台plc执行失败, 后台数据:" + string(boday))
		return "", grmi.NewBusinessError("读取数据后台plc执行失败, 后台数据:" + string(boday))
	}
	content, exist := plcData["Content"]
	if !exist {
		log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
		return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
	}
	if dataType == "String" {
		stringContent, ok := content.(string)
		if !ok {
			log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
			return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
		}
		if !ok {
			log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
			return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
		}
		length := len(stringContent)
		for index, item := range stringContent {
			if item == 0 {
				length = index
				break
			}
		}
		return stringContent[:length], nil
	} else if dataType == "Boolean" {
		boolContent, ok := content.(bool)
		if !ok {
			log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
			return false, grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday))
		}
		return boolContent, nil
	} else {
		return nil, grmi.NewBusinessError("不支持的数据类型")
	}
}

// WriteDataToPLC 向plc中写入数据
func WriteDataToPLC(data baseModel.PLC, log logger.Log) error {
	postData, err := json.Marshal(data)
	if err != nil {
		log.Error("序列化数据请求数据失败, error:" + err.Error())
		return grmi.NewBusinessError("序列化数据请求数据失败, error:" + err.Error())
	}
	log.Debug("写入数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend)
	boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData)
	if err != nil {
		log.Error("写入数据调用失败, error:" + err.Error())
		return grmi.NewBusinessError("写入数据调用失败, error:" + err.Error())
	}
	log.Debug("写入数据请求响应:" + string(boday))

	var response baseModel.Response
	err = json.Unmarshal(boday, &response)
	if err != nil {
		log.Error("解析写入数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday))
		return grmi.NewBusinessError("解析写入数据后台数据失败, error:" + err.Error())
	}
	if response.Code != iris.StatusOK {
		log.Error("写入数据调用后台失败, 后台返回错误, 后台数据:" + string(boday))
		return grmi.NewBusinessError("写入数据调用后台失败, 后台返回错误, 错误:" + response.Msg)
	}
	plcData, ok := response.Data.(map[string]interface{})
	if !ok {
		log.Error("写入数据后台返回data格式不正确, 后台数据:" + string(boday))
		return grmi.NewBusinessError("写入数据后台返回data格式不正确, 后台数据:" + string(boday))
	}
	successInterface, exist := plcData["IsSuccess"]
	if !exist {
		log.Error("写入数据后台返回data格式不正确, 后台数据:" + string(boday))
		return grmi.NewBusinessError("写入数据后台返回data格式不正确, 后台数据:" + string(boday))
	}
	success, ok := successInterface.(bool)
	if !ok {
		log.Error("写入数据后台返回data格式不正确, 后台数据:" + string(boday))
		return grmi.NewBusinessError("写入数据后台返回data格式不正确, 后台数据:" + string(boday))
	}
	if !success {
		log.Error("写入数据后台plc执行失败, 后台数据:" + string(boday))
		return grmi.NewBusinessError("写入数据后台plc执行失败, 后台数据:" + string(boday))
	}
	return nil
}