﻿using System.Collections.Generic;
using System.Linq;
using galaxy.core.communication.packets.outgoing.groups;
using galaxy.core.habbolib.cache.type;
using galaxy.core.habbolib.gameclients;
using galaxy.core.habbolib.groups;

namespace galaxy.core.communication.packets.incoming.groups
{
    class GetGroupMembersEvent : IPacketEvent
    {
        public void handle(GameClient Session, ClientPacket Packet)
        {
            int GroupId = Packet.PopInt();
            int Page = Packet.PopInt();
            string SearchVal = Packet.PopString();
            int RequestType = Packet.PopInt();

            Group g = null;
            if (!GalaxyEnvironment.GetGame().GetGroupManager().TryGetGroup(GroupId, out g))
                return;

            List<CachedUser> Members = new List<CachedUser>();

            switch (RequestType)
            {
                case 0:
                    {
                        List<int> MemberIds = g.GetAllMembers;
                        foreach (int Id in MemberIds.ToList())
                        {
                            CachedUser GroupMember = GalaxyEnvironment.GetGame().GetCacheManager().addUser(Id);
                            if (GroupMember == null)
                                continue;

                            if (!Members.Contains(GroupMember))
                                Members.Add(GroupMember);
                        }
                        break;
                    }

                case 1:
                    {
                        List<int> AdminIds = g.GetAdministrators;
                        foreach (int Id in AdminIds.ToList())
                        {
                            CachedUser GroupMember = GalaxyEnvironment.GetGame().GetCacheManager().addUser(Id);
                            if (GroupMember == null)
                                continue;

                            if (!Members.Contains(GroupMember))
                                Members.Add(GroupMember);
                        }
                        break;
                    }

                case 2:
                    {
                        List<int> RequestIds = g.GetRequests;
                        foreach (int Id in RequestIds.ToList())
                        {
                            CachedUser GroupMember = GalaxyEnvironment.GetGame().GetCacheManager().addUser(Id);
                            if (GroupMember == null)
                                continue;

                            if (!Members.Contains(GroupMember))
                                Members.Add(GroupMember);
                        }
                        break;
                    }
            }

            if (!string.IsNullOrEmpty(SearchVal))
                Members = Members.Where(x => x.username.StartsWith(SearchVal)).ToList();

            int StartIndex = ((Page - 1) * 14 + 14);
            int FinishIndex = Members.Count;

            Session.SendPacket(new GroupMembersComposer(g, Members.Skip(StartIndex).Take(FinishIndex - StartIndex).ToList(), Members.Count, Page, (g.CreatorId == Session.GetHabbo().Id || g.IsAdmin(Session.GetHabbo().Id)), RequestType, SearchVal));
        }
    }
}