package loging

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

type ResetLog struct {
	logical LogicalLog
	Date    string
}

func (s *ResetLog) SuccessLog(ctx context.Context, request request.AddLogRequest, date string) (err error) {
	code := strconv.Itoa(request.Coding)
	s.Date = date
	b, _ := s.logical.ExistsUuidCodeCache(ctx, s.Date, request.LogUuid, request.Coding, request.GameId)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.Date, request.LogUuid, request.Coding, request.GameId)
	}
	err = s.logical.PartTypeLogSetNum(ctx, s.Date, request.GameId, code, OkStatus, request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.NodeLogSetNum(ctx, s.Date, request.GameId, code, OkStatus, request.TaskType)
	return
}

func (s *ResetLog) FailLog(ctx context.Context, request request.AddLogRequest, date string) (err error) {
	code := strconv.Itoa(request.Coding)
	s.Date = date
	b, err := s.logical.ExistsUuidCodeCache(ctx, s.Date, request.LogUuid, request.Coding, request.GameId)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.Date, request.LogUuid, request.Coding, request.GameId)
	}
	if code[5:] == "00" {
		err = s.logical.CodeLogSetNum(ctx, s.Date, request.GameId, code, NoLogStatus, request.TaskType)
		return
	}
	err = s.logical.PartTypeLogSetNum(ctx, s.Date, request.GameId, code, FailStatus, request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.NodeLogSetNum(ctx, s.Date, request.GameId, code, FailStatus, request.TaskType)
	return
}

func (s *ResetLog) NoLogStatusData(ctx context.Context, request request.AddLogRequest, date string) (err error) {
	code := strconv.Itoa(request.Coding)
	s.Date = date
	b, _ := s.logical.ExistsUuidCodeCache(ctx, s.Date, request.LogUuid, request.Coding, request.TaskType)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.Date, request.LogUuid, request.Coding, request.TaskType)
	}
	err = s.logical.CodeLogSetNum(ctx, s.Date, request.GameId, code, NoLogStatus, request.TaskType)
	return
}
