﻿using galaxy.core.communication.packets.outgoing.rooms.engine;
using galaxy.core.database.interfaces;
using galaxy.core.habbolib.achievements;
using galaxy.core.habbolib.gameclients;
using galaxy.core.habbolib.items;
using galaxy.core.habbolib.quests;
using galaxy.core.habbolib.rooms;

namespace galaxy.core.communication.packets.incoming.rooms.engine
{
    class ApplyDecorationEvent : IPacketEvent
    {
        public void handle(GameClient Session, ClientPacket Packet)
        {
            if (!Session.GetHabbo().isInRoom)
                return;

            Room Room = null;
            if (!GalaxyEnvironment.GetGame().GetRoomManager().TryGetRoom(Session.GetHabbo().CurrentroomId, out Room))
                return;

            if (!Room.CheckRights(Session, true))
                return;

            Item Item = Session.GetHabbo().GetInventoryComponent().GetItem(Packet.PopInt());
            if (Item == null)
                return;

            if (Item.GetBaseItem() == null)
                return;

            string DecorationKey = string.Empty;
            switch (Item.GetBaseItem().InteractionType)
            {
                case InteractionType.floor:
                    DecorationKey = "floor";
                    break;

                case InteractionType.wallpaper:
                    DecorationKey = "wallpaper";
                    break;

                case InteractionType.landscape:
                    DecorationKey = "landscape";
                    break;
            }

            switch (DecorationKey)
            {
                case "floor":
                    Room.floor = Item.extraData;
                    Room.roomData.floor = Item.extraData;

                    GalaxyEnvironment.GetGame().GetQuestManager().ProgressUserQuest(Session, QuestType.FURNI_DECORATION_floor);
                    GalaxyEnvironment.GetGame().GetAchievementManager().giveAchievement(Session, AchievementGroup.ACH_RoomDecofloor, 1);
                    break;

                case "wallpaper":
                    Room.wallpaper = Item.extraData;
                    Room.roomData.wallpaper = Item.extraData;

                    GalaxyEnvironment.GetGame().GetQuestManager().ProgressUserQuest(Session, QuestType.FURNI_DECORATION_WALL);
                    GalaxyEnvironment.GetGame().GetAchievementManager().giveAchievement(Session, AchievementGroup.ACH_RoomDecowallpaper, 1);
                    break;

                case "landscape":
                    Room.landscape = Item.extraData;
                    Room.roomData.landscape = Item.extraData;

                    GalaxyEnvironment.GetGame().GetAchievementManager().giveAchievement(Session, AchievementGroup.ACH_RoomDecolandscape, 1);
                    break;
            }

            using (IQueryAdapter dbClient = GalaxyEnvironment.GetDatabaseManager().GetQueryReactor())
            {
                dbClient.SetQuery("UPDATE `rooms` SET `" + DecorationKey + "` = @extraData WHERE `id` = '" + Room.roomId + "' LIMIT 1");
                dbClient.AddParameter("extraData", Item.extraData);
                dbClient.RunQuery();

                dbClient.RunQuery("DELETE FROM `items` WHERE `id` = '" + Item.Id + "' LIMIT 1");
            }

            Session.GetHabbo().GetInventoryComponent().RemoveItem(Item.Id);
            Room.SendPacket(new RoomPropertyComposer(DecorationKey, Item.extraData));
        }
    }
}
