﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace _09.Teamwork_Projects
{
    public class StartUp
    {
        public static void Main()
        {
            var countOfTeams = int.Parse(Console.ReadLine());
            var createdTeams = new List<Team>();
            var listOfCreators = new Dictionary<string, string>();
            ReadTeamCreations(countOfTeams, createdTeams, listOfCreators);
            var joinRequsts = Console.ReadLine();
            joinRequsts = ReadJoinRequests(createdTeams, joinRequsts);
            var teamsToDisband = createdTeams.Where(x => x.Members.Count < 2).ToList();
            var formedTeams = createdTeams.Where(x => x.Members.Count >= 2).ToList();
            PrintFormedTeams(listOfCreators, formedTeams);
            PrintTeamsForDisband(teamsToDisband);
        }

        private static void ReadTeamCreations(int countOfTeams, List<Team> createdTeams,
            Dictionary<string, string> listOfCreators)
        {
            for (var i = 0; i < countOfTeams; i++)
            {
                var input = Console.ReadLine().Split('-');
                var creator = input[0];
                var teamName = input[1];
                var currentTeam = new Team {Name = teamName, Members = new List<string>()};
                var teamExists = CheckIfTeamExists(createdTeams, teamName);
                if (!teamExists)
                    CheckIfCreatorHasTeam(createdTeams, listOfCreators, creator, teamName, currentTeam);
                else Console.WriteLine($"Team {teamName} was already created!");
            }
        }

        private static void CheckIfCreatorHasTeam(List<Team> createdTeams, Dictionary<string, string> listOfCreators,
            string creator, string teamName, Team currentTeam)
        {
            if (listOfCreators.ContainsValue(creator))
            {
                Console.WriteLine($"{creator} cannot create another team!");
            }
            else
            {
                currentTeam.Members.Add(creator);
                createdTeams.Add(currentTeam);
                listOfCreators.Add(teamName, creator);
                Console.WriteLine($"Team {teamName} has been created by {creator}!");
            }
        }

        private static string ReadJoinRequests(List<Team> createdTeams, string joinRequsts)
        {
            while (joinRequsts != "end of assignment")
            {
                var input = joinRequsts.Split("->".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                var user = input[0];
                var teamName = input[1];
                var teamExists = CheckIfTeamExists(createdTeams, teamName);
                if (!teamExists)
                {
                    Console.WriteLine($"Team {teamName} does not exist!");
                }
                else
                {
                    var memberIsInTeam = CheckIfMemberIsAlreadyInTeam(createdTeams, user);
                    if (memberIsInTeam) Console.WriteLine($"Member {user} cannot join team {teamName}!");
                    else
                        for (var i = 0; i < createdTeams.Count; i++)
                            if (createdTeams[i].Name == teamName) createdTeams[i].Members.Add(user);
                }
                joinRequsts = Console.ReadLine();
            }

            return joinRequsts;
        }

        private static bool CheckIfMemberIsAlreadyInTeam(List<Team> createdTeams, string user)
        {
            var memberIsInTeam = false;
            for (var i = 0; i < createdTeams.Count; i++)
                if (createdTeams[i].Members.Contains(user))
                {
                    memberIsInTeam = true;
                    break;
                }

            return memberIsInTeam;
        }

        private static void PrintFormedTeams(Dictionary<string, string> listOfCreators, List<Team> formedTeams)
        {
            foreach (var team in formedTeams.OrderByDescending(x => x.Members.Count).ThenBy(x => x.Name))
            {
                Console.WriteLine(team.Name);
                Console.WriteLine($"- {listOfCreators[team.Name]}");
                foreach (var member in team.Members.OrderBy(x => x))
                    if (member != listOfCreators[team.Name])
                        Console.WriteLine($"-- {member}");
            }
        }

        private static void PrintTeamsForDisband(List<Team> teamsToDisband)
        {
            Console.WriteLine("Teams to disband:");
            foreach (var team in teamsToDisband.OrderByDescending(x => x.Members.Count).ThenBy(x => x.Name))
                Console.WriteLine(team.Name);
        }

        private static bool CheckIfTeamExists(List<Team> createdTeams, string teamName)
        {
            var teamExists = false;
            for (var i = 0; i < createdTeams.Count; i++)
                if (createdTeams[i].Name == teamName)
                {
                    teamExists = true;
                    break;
                }

            return teamExists;
        }

        public class Team
        {
            public string Name { get; set; }
            public List<string> Members { get; set; }
        }
    }
}