package net.cobem.grizzly.habbohotel.users.messenger;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.habbohotel.users.User;
import net.cobem.grizzly.habbohotel.users.UserDAO;
import net.cobem.grizzly.storage.DatabaseResult;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;


public class MessengerHandler {
    private int ID;
    private Map<Integer, User> Friends;
    private Map<Integer, User> FriendRequests;

    public MessengerHandler(int ID) {
        this.ID = ID;

        Friends = new HashMap<>();
        FriendRequests = new HashMap<>();

        try {
            LoadFriends();
            LoadFriendRequests();
        } catch (SQLException e) {
        }
    }

    private void LoadFriends() throws SQLException {
        DatabaseResult GrabFriends = Grizzly.getStorage().query("SELECT * FROM messenger_friendships WHERE user_one_id = '" + this.ID + "'");

        if (GrabFriends.getTable() != null) {
            ResultSet AsFirstFriend = GrabFriends.getTable();

            while (AsFirstFriend.next()) {
                Friends.put(
                        AsFirstFriend.getInt("id"),
                        UserDAO.generateUser(AsFirstFriend.getInt("user_two_id")));
            }
        }

        GrabFriends = Grizzly.getStorage().query("SELECT * FROM messenger_friendships WHERE user_two_id = '" + this.ID + "'");

        if (GrabFriends.getTable() != null) {
            ResultSet AsSecondFriend = GrabFriends.getTable();

            while (AsSecondFriend.next()) {
                Friends.put(
                        AsSecondFriend.getInt("id"),
                        UserDAO.generateUser(AsSecondFriend.getInt("user_one_id")));
            }
        }
    }

    private void LoadFriendRequests() throws SQLException {
        DatabaseResult GrabRequests = Grizzly.getStorage().query("SELECT * FROM messenger_requests WHERE to_id = '" + this.ID + "'");

        if (GrabRequests.getTable() != null) {
            ResultSet Pending = GrabRequests.getTable();

            while (Pending.next()) {
                FriendRequests.put(
                        Pending.getInt("id"),
                        UserDAO.generateUser(Pending.getInt("from_id")));
            }
        }
    }

    public Map<Integer, User> GrabFriends() {
        return Friends;
    }

    public Map<Integer, User> GrabRequests() {
        return FriendRequests;
    }

    public boolean IsFriend(int ID) {
        return Friends.containsValue(UserDAO.generateUser(ID));
    }

    public void UpdateStatus(boolean Online) {
        EventResponse Message = new EventResponse();
        Session Session = Grizzly.getHabboHotel().getSessions().getByID(this.ID);

        Message.Initialize(HeaderLibrary.UpdateFriendStateEvent);
        Message.addInt(0);
        Message.addInt(1);
        Message.addInt(0);
        Message.addInt(Session.getHabbo().getID());
        Message.addString(Session.getHabbo().getUsername());
        Message.addInt(1);
        Message.addBool(Online);
        Message.addBool(false);
        Message.addString(Session.getHabbo().getLook());
        Message.addInt(0);
        Message.addString(Session.getHabbo().getMotto());
        Message.addInt(0);
        Message.addInt(0);
        Message.addInt(0);

        ChannelGroup Friends = new DefaultChannelGroup();

        for (User mUser : this.GrabFriends().values()) {
            Session mSession = Grizzly.getHabboHotel().getSessions().getByID(mUser.getID());

            if (mSession == null) {
                continue;
            }

            if (mSession.getHabbo().getID() == this.ID) {
                continue;
            }

            Friends.add(mSession.getChannel());
        }

        Friends.write(Message);
    }
}
