package services

import (
	"evening_detective/internal/models"
	story_service_models "evening_detective/internal/services/story_service/models"
	"evening_detective/proto"
	"strings"
)

func mapTeamsToTeamAdvanced(team *models.Team) *proto.TeamAdvanced {
	return &proto.TeamAdvanced{
		Id:       team.ID,
		Name:     team.Name,
		Password: team.Password,
	}
}

func mapTeamsToTeamFull(team *models.Team) *proto.TeamFull {
	return &proto.TeamFull{
		Id:       team.ID,
		Name:     team.Name,
		Password: team.Password,
	}
}

func mapProtoTeamsToTeam(team *proto.Team) *models.Team {
	return &models.Team{
		Name: clearTeamName(team.Name),
	}
}

func mapPlaceToProtoAction(place *story_service_models.Place) *proto.Action {
	return &proto.Action{
		Place: place.Code,
	}
}

func mapStoryApplicationToProtoApplication(application *story_service_models.Application) *proto.Application {
	return &proto.Application{
		Name: application.Name,
		Number: application.Number,
	}
}

func mapStoryDoorToProtoDoor(door *story_service_models.Door) *proto.Door {
	return &proto.Door{
		Code: door.Code,
		Name: door.Name,
		Show: door.Show,
	}
}

func mapStoryApplicationsToApplications(applications []*story_service_models.Application) []*models.Application {
	res := make([]*models.Application, 0, len(applications))
	for _, application := range applications {
		res = append(res, mapStoryApplicationToApplication(application))
	}
	return res
}

func mapStoryApplicationToApplication(application *story_service_models.Application) *models.Application {
	return &models.Application{
		Name:  application.Name,
		State: "NEW",
	}
}

func mapApplicationsToProtoApplications(applications []*models.Application) []*proto.Application {
	res := make([]*proto.Application, 0, len(applications))
	for _, application := range applications {
		res = append(res, mapApplicationToProtoApplication(application))
	}
	return res
}

func mapApplicationToProtoApplication(application *models.Application) *proto.Application {
	return &proto.Application{
		Id:    application.ID,
		Name:  application.Name,
		State: application.State,
	}
}

func mapProtoApplicationsToApplications(items []*proto.Application) []*models.Application {
	res := make([]*models.Application, 0, len(items))
	for _, item := range items {
		res = append(res, mapProtoApplicationToApplication(item))
	}
	return res
}

func mapProtoApplicationToApplication(items *proto.Application) *models.Application {
	return &models.Application{
		ID: items.Id,
	}
}

func clearTeamName(code string) string {
	return strings.TrimSpace(code)
}
