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.habbohotel.sessions.Session;
import net.cobem.grizzly.storage.DatabaseResult;

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

public class FloorItem implements iSerializeEvent {
    public int ID;
    public int Base;
    public int X;
    public int Y;
    public int Rotation;
    public int Room;

    public Float Height;
    public String StringHeight;
    public String ExtraData;

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

    public int UpdateCounter = 0;

    public Session Interactor = null;

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

    private boolean FillClass(ResultSet Set) {
        try {
            this.ID = Set.getInt("id");
            this.Base = Set.getInt("base_item");
            this.X = Set.getInt("x");
            this.Y = Set.getInt("y");
            this.Rotation = Set.getInt("rot");
            this.Room = Set.getInt("room_id");

            this.Height = Set.getFloat("z");
            this.StringHeight = "" + this.Height;
            this.ExtraData = Set.getString("extra_data");

            return true;
        } catch (SQLException Ex) {
            Grizzly.write(Ex.toString());
            return false;
        }
    }

    public FloorItem(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.addInt(ID);
        Message.addInt(getBase().Sprite);
        Message.addInt(X);
        Message.addInt(Y);
        Message.addInt(Rotation);
        Message.addString(Float.toString(Height));

        if (getBase().ItemTitle.equals("ads_background") && ExtraData.length() > 10) {
            Message.addInt(0);
            Message.addInt(1);

            if (!ExtraData.equals("")) {
                String[] Split = ExtraData.split("  ");
                Message.addInt(5);

                for (int i = 0; i <= (Split.length - 1); i++) {
                    Message.addString(Split[i].trim());
                }
            } else {
                Message.addInt(0);
            }

            Message.addInt(-1);
            Message.addInt(1);
        } else {
            Message.addInt(0);
            Message.addInt(0);
            Message.addString(ExtraData);
            Message.addInt(-1);
            Message.addInt(getBase().Interaction.equals("default") ? 1 : 0);
        }
    }

    /**
     * @source: Shynoshy + Alex
     */
    public boolean changeState() {
        if ((this.getBase().Interaction.equals("default") || this.getBase().Interaction.equals("gate")) && (this.getBase().InteractionModesCount > 1)) {
            if (ExtraData.isEmpty() || ExtraData.equals(" ")) {
                ExtraData = "0";
            }

            Integer Temp = Integer.parseInt(ExtraData) + 1;

            if (Temp > this.getBase().InteractionModesCount) {
                ExtraData = "0";
            } else {
                ExtraData = "" + Temp;
            }

            return true;
        } else {
            return false;
        }
    }


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

    public void serializeUpdate(Session Session) {
        Session.getResponse().Initialize(HeaderLibrary.UpdateFloorExtraDataEvent);
        Session.getResponse().addString(ID);
        Session.getResponse().addInt(0);
        Session.getResponse().addString(ExtraData);
        Session.getActor().CurrentRoom.sendMessage(Session.getResponse());
    }

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

        Message.Initialize(HeaderLibrary.UpdateFloorExtraDataEvent);
        Message.addString(ID);
        Message.addInt(0);
        Message.addString(ExtraData);
        CurrentRoom.sendMessage(Message);
    }

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

            this.UpdateCounter--;

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