use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use otg_core::ui::ChatElement;

#[derive(Default)]
pub struct ChatPrivate {
    chat_history: gtk::Box,
    entry: gtk::Entry,
}

#[glib::object_subclass]
impl ObjectSubclass for ChatPrivate {
    const NAME: &'static str = "Chat";
    type Type = Chat;
    type ParentType = gtk::Box;
}

impl ObjectImpl for ChatPrivate {
    fn constructed(&self) {
        self.chat_history
            .set_orientation(gtk::Orientation::Vertical);
    }
}
impl WidgetImpl for ChatPrivate {}
impl BoxImpl for ChatPrivate {}

glib::wrapper! {
    pub struct Chat(ObjectSubclass<ChatPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}

impl Chat {
    pub fn new(element: ChatElement) -> Chat {
        let s: Self = Object::builder().build();
        s.set_orientation(gtk::Orientation::Vertical);

        s.append(&s.imp().chat_history);
        s.append(&s.imp().entry);

        element.messages.into_iter().for_each(|msg| {
            s.imp().chat_history.append(
                &gtk::Label::builder()
                    .label(msg)
                    .halign(gtk::Align::Start)
                    .build(),
            )
        });

        s
    }
}
