package loging

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

type ScriptStartLog struct {
	logical LogicalLog
}

func (s *ScriptStartLog) SuccessLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 1
	s.logical.ScriptType = request.ScriptType
	s.logical.Request = request
	err = s.logical.DataAdd()
	if err != nil {
		return
	}
	if s.logical.ScriptType == 1 {
		return
	}
	code := strconv.Itoa(request.Coding)
	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 *ScriptStartLog) FailLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 0
	s.logical.ScriptType = request.ScriptType
	s.logical.Request = request
	err = s.logical.DataAdd()
	if err != nil {
		return
	}
	if s.logical.ScriptType == 1 {
		return
	}
	code := strconv.Itoa(request.Coding)
	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 *ScriptStartLog) 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
}
