package net.cobem.grizzly.habbohotel.rooms.items;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;
import net.cobem.grizzly.communication.iSerializeEvent;
import net.cobem.grizzly.habbohotel.furni.Furniture;
import net.cobem.grizzly.habbohotel.rooms.Room;
import net.cobem.grizzly.storage.DatabaseResult;

import java.sql.ResultSet;
import java.sql.SQLException;

public class WallItem implements iSerializeEvent {
    public int ID;
    public int Base;
    public int Room;

    public String Position;
    public String ExtraData;

    //Idea taken from uber; And might I say, damn good idea.
    public boolean UpdateNeeded = false;
    public int UpdateCounter = 0;

    public WallItem(ResultSet Set) {
        this.FillClass(Set);
    }

    private boolean FillClass(ResultSet Set) {
        try {
            this.ID = Set.getInt("id");
            this.Base = Set.getInt("base_item");
            this.ExtraData = Set.getString("extra_data");
            this.Position = Set.getString("wall_pos");
            this.Room = Set.getInt("room_id");
            return true;
        } catch (SQLException Ex) {
            Grizzly.write(Ex.toString());
            return false;
        }
    }

    public WallItem(int ID) {
        DatabaseResult GrabItem = Grizzly.getStorage().query("SELECT * FROM items WHERE id = '" + ID + "'");

        ResultSet Results = GrabItem.getRow();

        FillClass(Results);
    }

    public Furniture getBase() {
        return Grizzly.getHabboHotel().getFurniture().getByID(Base);
    }

    public void serialize(EventResponse Message) {
        Message.addString(ID);
        Message.addInt(getBase().Sprite);
        Message.addString(Position);
        Message.addString(ExtraData);
        Message.addInt(getBase().Interaction.equals("default") ? 1 : 0);
    }

    public void serializeUpdate(Room CurrentRoom) {
        EventResponse Message = new EventResponse();

        Message.Initialize(HeaderLibrary.UpdateWallExtraDataEvent);
        Message.addBody(this);
        Message.addInt(0);
        Message.addString(CurrentRoom.OwnerByName);
        CurrentRoom.sendMessage(Message);
    }

    /**
     * @source: Shynoshy + Alex
     */
    public boolean changeState() {
        if (ExtraData.isEmpty()) {
            ExtraData = "1";
        } else {
            if (ExtraData.contains("1")) {
                ExtraData = "0";
            } else if (ExtraData.contains("0")) {
                ExtraData = "1";
            }
        }
        return true;
    }


    /**
     * @source: Shynoshy + Alex
     */
    public void saveState() {
        Grizzly.getStorage().execute("UPDATE items SET extra_data = '" + ExtraData + "' WHERE id = '" + ID + "'");
    }

    public void cycle() {
        if (this.UpdateNeeded && this.UpdateCounter >= 1) {
            Grizzly.getHabboHotel().getInteractions().onCycle(this.getBase().Interaction, this);

            this.UpdateCounter--;

            if (this.UpdateCounter <= 0) {
                this.UpdateNeeded = false;
            }
        }
    }
}
