package NatsServer

import (
	"ColdVerify_local/conf"
	"ColdVerify_local/lib"
	"ColdVerify_local/logs"
	"ColdVerify_local/models/Account"
	"ColdVerify_local/models/Device"
	"ColdVerify_local/models/Task"
	"errors"
	"github.com/nats-io/nats.go"
	"github.com/vmihailenco/msgpack/v5"
	"time"
)

func Update_Task(v Task.Task) error {
	logs.Debug("Nats =>", lib.FuncName(), v)

	b, err := msgpack.Marshal(&v)
	if err != nil {
		return err
	}

	msg, err := lib.Nats.Request("ColdVerify_Server_Update_Task", b, 3*time.Second)
	if err != nil {
		return err
	}

	type T_R struct {
		Code int16     `xml:"Code"`
		Msg  string    `xml:"Msg"`
		Data Task.Task `xml:"Data"`
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return err
	}
	if t_R.Code != 200 {
		return errors.New(t_R.Msg)
	}

	return nil

}

func Read_Task(T_task_id string) (task Task.Task, err error) {
	//logs.Println("Nats =>", lib.FuncName(), T_task_id)

	if lib.Nats == nil || !lib.Nats.IsConnected() {

		lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
		if err != nil {
			logs.Println("无法重新连接到 NATS:", err)
		} else {
			logs.Println("成功重新连接到 NATS...")
		}
	}

	msg, err := lib.Nats.Request("ColdVerify_Server_Read_Task", []byte(T_task_id), 3*time.Second)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return task, err
	}

	type T_R struct {
		Code int16     `xml:"Code"`
		Msg  string    `xml:"Msg"`
		Data Task.Task `xml:"Data"`
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return task, err
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		logs.Error(lib.FuncName(), err.Error())
		return task, err
	}

	return t_R.Data, nil

}
func Read_User(T_uuid string) (user Account.User, err error) {
	logs.Println("Nats =>", lib.FuncName(), T_uuid)

	msg, err := lib.Nats.Request("ColdVerify_Server_Read_User", []byte(T_uuid), 3*time.Second)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return user, err
	}

	type T_R struct {
		Code int16        `xml:"Code"`
		Msg  string       `xml:"Msg"`
		Data Account.User `xml:"Data"`
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return user, err
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		logs.Error(lib.FuncName(), err.Error())
		return user, err
	}

	return t_R.Data, nil

}
func Read_Admin(T_uuid string) (user Account.Admin, err error) {
	logs.Println("Nats =>", lib.FuncName(), T_uuid)

	msg, err := lib.Nats.Request("ColdVerify_Server_Read_Admin", []byte(T_uuid), 3*time.Second)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return user, err
	}

	type T_R struct {
		Code int16         `xml:"Code"`
		Msg  string        `xml:"Msg"`
		Data Account.Admin `xml:"Data"`
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return user, err
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		logs.Error(lib.FuncName(), err.Error())
		return user, err
	}

	return t_R.Data, nil

}
func Device_Class_List(T_task_id string) (list []Device.DeviceClassList, err error) {
	//logs.Println("Nats =>", lib.FuncName(), T_task_id)

	if !lib.Nats.IsConnected() {
		lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
		if err != nil {
			logs.Println("无法重新连接到 NATS:", err)
		}
		logs.Println("成功重新连接到 NATS...")
	}

	msg, err := lib.Nats.Request("ColdVerify_Server_Device_Class_List", []byte(T_task_id), 3*time.Second)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return list, err
	}

	type T_R struct {
		Code int16                    `xml:"Code"`
		Msg  string                   `xml:"Msg"`
		Data []Device.DeviceClassList `xml:"Data"`
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		logs.Error(lib.FuncName(), err.Error())
		return list, err
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		logs.Error(lib.FuncName(), err.Error())
		return list, err
	}

	return t_R.Data, nil

}
func Add_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
	logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
	type T_R struct {
		T_task_id string `xml:"T_task_id"` // 任务主键id
		T_sn      string `xml:"T_sn"`
		T_id      string `xml:"T_id"`
	}

	t_R := T_R{
		T_task_id: T_task_id,
		T_sn:      T_sn,
		T_id:      T_id,
	}

	b, err := msgpack.Marshal(&t_R)
	if err != nil {
		return err
	}

	msg, err := lib.Nats.Request("ColdVerify_Server_Add_DeviceClassList", b, 3*time.Second)
	if err != nil {
		return err
	}

	type T_Res struct {
		Code int16  `xml:"Code"`
		Msg  string `xml:"Msg"`
	}
	var t_Res T_Res

	err = msgpack.Unmarshal(msg.Data, &t_Res)
	if err != nil {
		return err
	}
	if t_Res.Code != 200 {
		return errors.New(t_Res.Msg)
	}

	return nil

}
func Edit_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
	logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
	type T_R struct {
		T_task_id string `xml:"T_task_id"` // 任务主键id
		T_sn      string `xml:"T_sn"`
		T_id      string `xml:"T_id"`
	}

	t_R := T_R{
		T_task_id: T_task_id,
		T_sn:      T_sn,
		T_id:      T_id,
	}

	b, err := msgpack.Marshal(&t_R)
	if err != nil {
		return err
	}

	msg, err := lib.Nats.Request("ColdVerify_Server_Edit_DeviceClassList", b, 3*time.Second)
	if err != nil {
		return err
	}

	type T_Res struct {
		Code int16  `xml:"Code"`
		Msg  string `xml:"Msg"`
	}
	var t_Res T_Res

	err = msgpack.Unmarshal(msg.Data, &t_Res)
	if err != nil {
		return err
	}
	if t_Res.Code != 200 {
		return errors.New(t_Res.Msg)
	}

	return nil

}
func Del_TaskData_ByT_BindDeviceDataTime(T_task_id string) {
	logs.Debug("Nats =>", lib.FuncName(), T_task_id)
	err := lib.Nats.Publish("ColdVerify_Server_Del_TaskData_ByT_BindDeviceDataTime", []byte(T_task_id))
	if err != nil {
		logs.Error("Nats =>", lib.FuncName(), err)
	}

}
