using Gtk;

public class TextBubbleLayout : BubbleLayout
{
    public Message message; 
    private Pango.Layout layout;

    public static float line_height { get { return 1.18f; } }

    public static Pango.FontDescription body_font { 
        owned get {
            var settings = Gtk.Settings.get_default();
            var font_name = settings.gtk_font_name;

            // Create font description from system font
            var font_desc = Pango.FontDescription.from_string(font_name);
            return font_desc;
        }
    }

    public TextBubbleLayout(Message message, Widget parent, float max_width) {
        base(parent, max_width);

        this.from_me = message.from_me;
        this.message = message;

        layout = parent.create_pango_layout(null);
        layout.set_markup(message.markup, -1);

        var font_desc = TextBubbleLayout.body_font;
        layout.set_font_description(font_desc);
        layout.set_wrap(Pango.WrapMode.WORD_CHAR);
        layout.set_line_spacing(line_height);

        // Set max width 
        layout.set_width((int)text_available_width * Pango.SCALE);
    }

    private float text_available_width {
        get {
            return max_width - text_x_offset - constants.text_x_padding;
        }
    }

    private float text_x_offset {
        get {
            return from_me ? constants.text_x_padding : constants.tail_width + constants.text_x_padding;
        }
    }

    private float text_x_padding {
        get {
            // Opposite of text_x_offset
            return from_me ? constants.tail_width + constants.text_x_padding : constants.text_x_padding;
        }
    }

    private Gdk.RGBA background_color {
        get { 
            return from_me ? parent.get_color() : Gdk.RGBA() { 
                red = 1.0f, 
                green = 1.0f, 
                blue = 1.0f, 
                alpha = 0.08f 
            };
        }
    }

    public override float get_height() {
        Pango.Rectangle ink_rect, logical_rect;
        layout.get_pixel_extents(out ink_rect, out logical_rect);

        return logical_rect.height + constants.corner_radius + constants.tail_bottom_padding + constants.text_y_padding;
    }

    public override float get_width() {
        Pango.Rectangle ink_rect, logical_rect;
        layout.get_pixel_extents(out ink_rect, out logical_rect);

        return logical_rect.width + text_x_offset + text_x_padding;
    }

    public Graphene.Point get_text_origin() {
        Pango.Rectangle ink_rect, logical_rect;
        layout.get_pixel_extents(out ink_rect, out logical_rect);
        
        return Graphene.Point() { 
            x = text_x_offset, 
            y = ((get_height() - constants.tail_bottom_padding) - logical_rect.height) / 2 
        };
    }

    public override void draw_content(Snapshot snapshot) {
        snapshot.save();

        snapshot.translate(get_text_origin());

        snapshot.append_layout(layout, Gdk.RGBA() { 
            red = 1.0f, 
            green = 1.0f, 
            blue = 1.0f, 
            alpha = 1.0f 
        });

        snapshot.restore();
    }

    public override void copy(Gdk.Clipboard clipboard) {
        clipboard.set_text(message.text);
    }
}


