#pragma once

#include "CBaseUIScrollView.h"
#include "CBaseUITextbox.h"
#include "OsuScreen.h"


class CBaseUIButton;
class McFont;
class OsuChat;
class OsuUIButton;


struct ChatMessage {
    time_t tms;
    int32_t author_id;
    UString author_name;
    UString text;
};

struct OsuChatChannel {
    OsuChatChannel(OsuChat* chat, UString name_arg);
    ~OsuChatChannel();

    OsuChat* m_chat;
    CBaseUIScrollView* ui;
    OsuUIButton *btn;
    UString name;
    std::vector<ChatMessage> messages;
    float y_total;
    bool read = true;

    void add_message(ChatMessage msg);
    void updateLayout(Vector2 pos, Vector2 size);
    void onChannelButtonClick(CBaseUIButton* btn);
};

class OsuChat : public OsuScreen {
public:
    OsuChat(Osu *osu);

    virtual void draw(Graphics *g);
    virtual void mouse_update(bool *propagate_clicks);
    virtual void onKeyDown(KeyboardEvent &e);
    virtual void onKeyUp(KeyboardEvent &e);
    virtual void onChar(KeyboardEvent &e);
    virtual void onResolutionChange(Vector2 newResolution);

    void mark_as_read(OsuChatChannel* chan);
    void switchToChannel(OsuChatChannel* chan);
    void addChannel(UString channel_name, bool switch_to = false);
    void addMessage(UString channel_name, ChatMessage msg);
    void removeChannel(UString channel_name);
    void updateLayout(Vector2 newResolution);
    void updateButtonLayout(Vector2 screen);

    void join(UString channel_name);
    void leave(UString channel_name);
    void onDisconnect();

    virtual CBaseUIContainer* setVisible(bool visible);
    bool isVisibilityForced();
    void updateVisibility();
    bool isMouseInChat();

    void askWhatChannelToJoin(CBaseUIButton *btn);
    OsuUIButton *join_channel_btn;

    OsuChatChannel* m_selected_channel = nullptr;
    std::vector<OsuChatChannel*> m_channels;
    CBaseUIContainer *m_button_container;
    CBaseUITextbox *m_input_box;

    McFont* font;
    float m_fAnimation = 0.f;
    bool user_wants_chat = false;
    bool visibility_was_forced = false;

    const float input_box_height = 30.f;
    const float button_height = 26.f;
};
