package loging

import (
	"context"
	"log-server/model/log/request"
	"strconv"
)

type SimulatorStartLog struct {
	logical LogicalLog
}

func (s *SimulatorStartLog) SuccessLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 1
	s.logical.Request = request
	s.logical.ScriptType = request.ScriptType
	err = s.logical.DataAdd()
	s.logical.AddDeviceErrLog(s.logical.Request, 2)
	if err != nil {
		if s.logical.Request.SimulatorIp == "" {
			return
		}
		s.logical.Request.Err = s.logical.Request.SimulatorIp[:3]
		s.logical.AddDeviceErrLog(s.logical.Request, 1)
		return
	}
	code := strconv.Itoa(request.Coding)
	b, _ := s.logical.ExistsUuidCodeCache(ctx, s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	}
	s.logical.AddIpLog(s.logical.Request)
	s.logical.AddDeviceLog(s.logical.Request)
	err = s.logical.PartTypeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, OkStatus, s.logical.Request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.NodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, OkStatus, s.logical.Request.TaskType)
	return
}

func (s *SimulatorStartLog) FailLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 0
	s.logical.Request = request
	s.logical.ScriptType = request.ScriptType
	err = s.logical.DataAdd()
	if err != nil {
		return
	}
	code := strconv.Itoa(request.Coding)
	b, _ := s.logical.ExistsUuidCodeCache(ctx, s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	}
	if code[5:] == "00" {
		s.logical.Status = 3
		err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, strconv.Itoa(request.Coding), NoLogStatus, s.logical.Request.TaskType)
		return
	}
	err = s.logical.PartTypeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, FailStatus, s.logical.Request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.NodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, FailStatus, s.logical.Request.TaskType)
	return
}

func (s *SimulatorStartLog) NoLogStatusData(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 2
	s.logical.Request = request
	err = s.logical.NoLogStatusDataAdd(ctx)
	if err != nil {
		return
	}
	err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, strconv.Itoa(request.Coding), NoLogStatus, s.logical.Request.TaskType)
	return
}
