package net.cobem.grizzly.habbohotel.games.soccer;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;
import net.cobem.grizzly.habbohotel.pathfinding.Position;
import net.cobem.grizzly.habbohotel.rooms.actors.Actor;
import net.cobem.grizzly.habbohotel.rooms.items.FloorItem;
import net.cobem.grizzly.habbohotel.sessions.Session;

import java.util.ArrayList;
import java.util.List;

public class BallThread implements Runnable {
    private boolean Rolling;
    private Thread Roller;

    private FloorItem RoomItem;
    private List<Position> Path = new ArrayList<>();
    private Session Session;
    private Actor Actor;

    private Position ItemPosition;

    public BallThread(FloorItem Item, List<Position> Path, Session User) {
        this.RoomItem = Item;
        this.Path = Path;
        this.Session = User;

        this.Actor = Session.getActor();
        this.ItemPosition = new Position(Item.X, Item.Y, Item.Height);

        Roller = new Thread(this);
        Roller.start();
        Rolling = true;
    }

    @Override
    public void run() {
        while (Rolling) {
            for (Position New : Path) {
                if (New == Path.get(Path.size() - 1)) {
                    Grizzly.getStorage().execute("UPDATE server_items SET x = '" + New.X + "', y = '" + New.Y + "', rotation = '" + RoomItem.Rotation + "', height = '" + RoomItem.Height + "' WHERE id = '" + RoomItem.ID + "'");
                }

                EventResponse Message = new EventResponse();

                Message.Initialize(HeaderLibrary.BallRollAnimationEvent);
                Message.addInt(RoomItem.ID);
                Message.addInt(RoomItem.getBase().Sprite);
                Message.addInt(ItemPosition.X);
                Message.addInt(ItemPosition.Y);
                Message.addInt(RoomItem.Rotation);
                Message.addString(RoomItem.Height.toString().replace(',', '.'));
                Message.addInt(0);
                Message.addInt(0);
                Message.addString(Path.size() * 11);
                Message.addInt(-1);
                Message.addInt(0);
                Message.addInt(753651);

                Session.getActor().CurrentRoom.sendMessage(Message);

                Message.Initialize(HeaderLibrary.UpdateItemOnRollerEvent);
                Message.addInt(ItemPosition.X);
                Message.addInt(ItemPosition.Y);
                Message.addInt(New.X);
                Message.addInt(New.Y);
                Message.addInt(1);
                Message.addInt(RoomItem.ID);
                Message.addString(RoomItem.Height.toString().replace(',', '.'));
                Message.addString(Double.toString(Actor.CurrentRoom.getModel().getTileHeight(New.X, New.Y)).replace(',', '.'));
                Message.addInt(0);

                Actor.CurrentRoom.getFloors().remove(RoomItem.ID);
                Actor.CurrentRoom.getFloors().put(RoomItem.ID, new FloorItem(RoomItem.ID));

                Session.getActor().CurrentRoom.sendMessage(Message);

                ItemPosition = new Position(New.X, New.Y, RoomItem.Height);

                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }
            }

            Rolling = false;
        }
    }
}
