use adw::prelude::*;

pub fn build_processing_page() -> adw::NavigationPage {
    let content = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .spacing(16)
        .margin_top(24)
        .margin_bottom(24)
        .margin_start(24)
        .margin_end(24)
        .vexpand(true)
        .build();

    // Title
    let title = gtk::Label::builder()
        .label("Processing...")
        .css_classes(["title-1"])
        .halign(gtk::Align::Start)
        .build();

    // Progress info
    let progress_label = gtk::Label::builder()
        .label("0 / 0 images")
        .css_classes(["heading"])
        .halign(gtk::Align::Start)
        .build();
    progress_label.set_widget_name("processing-count-label");

    let progress_bar = gtk::ProgressBar::builder()
        .fraction(0.0)
        .show_text(true)
        .text("0%")
        .build();
    progress_bar.update_property(&[
        gtk::accessible::Property::Label("Processing progress"),
    ]);

    let eta_label = gtk::Label::builder()
        .label("Estimating time remaining...")
        .css_classes(["dim-label"])
        .halign(gtk::Align::Start)
        .build();
    eta_label.set_widget_name("processing-eta-label");

    // Activity log
    let log_group = adw::PreferencesGroup::builder()
        .title("Activity")
        .build();

    let log_scrolled = gtk::ScrolledWindow::builder()
        .hscrollbar_policy(gtk::PolicyType::Never)
        .vexpand(true)
        .min_content_height(200)
        .build();

    let log_box = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .spacing(2)
        .build();
    log_box.set_widget_name("processing-log-box");

    log_scrolled.set_child(Some(&log_box));
    log_group.add(&log_scrolled);

    // Control buttons
    let button_box = gtk::Box::builder()
        .orientation(gtk::Orientation::Horizontal)
        .spacing(12)
        .halign(gtk::Align::Center)
        .margin_top(12)
        .build();

    let pause_button = gtk::Button::builder()
        .label("Pause")
        .tooltip_text("Pause after current image")
        .build();

    let cancel_button = gtk::Button::builder()
        .label("Cancel")
        .tooltip_text("Cancel processing")
        .build();
    cancel_button.add_css_class("destructive-action");

    button_box.append(&pause_button);
    button_box.append(&cancel_button);

    content.append(&title);
    content.append(&progress_label);
    content.append(&progress_bar);
    content.append(&eta_label);
    content.append(&log_group);
    content.append(&button_box);

    adw::NavigationPage::builder()
        .title("Processing")
        .tag("processing")
        .child(&content)
        .build()
}

pub fn build_results_page() -> adw::NavigationPage {
    let scrolled = gtk::ScrolledWindow::builder()
        .hscrollbar_policy(gtk::PolicyType::Never)
        .vexpand(true)
        .build();

    let content = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .spacing(16)
        .margin_top(24)
        .margin_bottom(24)
        .margin_start(24)
        .margin_end(24)
        .build();

    // Success icon and title
    let status_icon = gtk::Image::builder()
        .icon_name("emblem-ok-symbolic")
        .pixel_size(48)
        .halign(gtk::Align::Center)
        .css_classes(["success"])
        .build();
    status_icon.update_property(&[
        gtk::accessible::Property::Label("Success"),
    ]);

    let title = gtk::Label::builder()
        .label("Processing Complete")
        .css_classes(["title-1"])
        .halign(gtk::Align::Center)
        .build();

    content.append(&status_icon);
    content.append(&title);

    // Stats
    let stats_group = adw::PreferencesGroup::builder()
        .title("Results")
        .build();

    let images_row = adw::ActionRow::builder()
        .title("Images processed")
        .subtitle("0 images")
        .build();
    let images_icon = gtk::Image::from_icon_name("image-x-generic-symbolic");
    images_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    images_row.add_prefix(&images_icon);

    let size_before_row = adw::ActionRow::builder()
        .title("Original size")
        .subtitle("0 B")
        .build();
    let size_before_icon = gtk::Image::from_icon_name("drive-harddisk-symbolic");
    size_before_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    size_before_row.add_prefix(&size_before_icon);

    let size_after_row = adw::ActionRow::builder()
        .title("Output size")
        .subtitle("0 B")
        .build();
    let size_after_icon = gtk::Image::from_icon_name("drive-harddisk-symbolic");
    size_after_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    size_after_row.add_prefix(&size_after_icon);

    let savings_row = adw::ActionRow::builder()
        .title("Space saved")
        .subtitle("0%")
        .build();
    let savings_icon = gtk::Image::from_icon_name("emblem-ok-symbolic");
    savings_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    savings_row.add_prefix(&savings_icon);

    let time_row = adw::ActionRow::builder()
        .title("Processing time")
        .subtitle("0s")
        .build();
    let time_icon = gtk::Image::from_icon_name("preferences-system-time-symbolic");
    time_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    time_row.add_prefix(&time_icon);

    stats_group.add(&images_row);
    stats_group.add(&size_before_row);
    stats_group.add(&size_after_row);
    stats_group.add(&savings_row);
    stats_group.add(&time_row);
    content.append(&stats_group);

    // Errors section (initially hidden)
    let errors_group = adw::PreferencesGroup::builder()
        .title("Errors")
        .visible(false)
        .build();

    let errors_expander = adw::ExpanderRow::builder()
        .title("0 errors occurred")
        .build();
    errors_group.add(&errors_expander);
    content.append(&errors_group);

    // Action buttons
    let action_group = adw::PreferencesGroup::new();

    let open_row = adw::ActionRow::builder()
        .title("Open Output Folder")
        .subtitle("View processed images in file manager")
        .activatable(true)
        .build();
    let open_icon = gtk::Image::from_icon_name("folder-open-symbolic");
    open_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    open_row.add_prefix(&open_icon);
    let open_arrow = gtk::Image::from_icon_name("go-next-symbolic");
    open_arrow.set_accessible_role(gtk::AccessibleRole::Presentation);
    open_row.add_suffix(&open_arrow);

    let process_more_row = adw::ActionRow::builder()
        .title("Process Another Batch")
        .subtitle("Start over with new images")
        .activatable(true)
        .build();
    let more_icon = gtk::Image::from_icon_name("view-refresh-symbolic");
    more_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    process_more_row.add_prefix(&more_icon);
    let more_arrow = gtk::Image::from_icon_name("go-next-symbolic");
    more_arrow.set_accessible_role(gtk::AccessibleRole::Presentation);
    process_more_row.add_suffix(&more_arrow);

    let save_preset_row = adw::ActionRow::builder()
        .title("Save as Preset")
        .subtitle("Save this workflow for future use")
        .activatable(true)
        .build();
    let save_icon = gtk::Image::from_icon_name("document-save-symbolic");
    save_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    save_preset_row.add_prefix(&save_icon);
    let save_arrow = gtk::Image::from_icon_name("go-next-symbolic");
    save_arrow.set_accessible_role(gtk::AccessibleRole::Presentation);
    save_preset_row.add_suffix(&save_arrow);

    let add_queue_row = adw::ActionRow::builder()
        .title("Add to Queue")
        .subtitle("Queue another batch with different images")
        .activatable(true)
        .build();
    let queue_icon = gtk::Image::from_icon_name("view-list-symbolic");
    queue_icon.set_accessible_role(gtk::AccessibleRole::Presentation);
    add_queue_row.add_prefix(&queue_icon);
    let queue_arrow = gtk::Image::from_icon_name("go-next-symbolic");
    queue_arrow.set_accessible_role(gtk::AccessibleRole::Presentation);
    add_queue_row.add_suffix(&queue_arrow);

    action_group.add(&open_row);
    action_group.add(&process_more_row);
    action_group.add(&add_queue_row);
    action_group.add(&save_preset_row);
    content.append(&action_group);

    scrolled.set_child(Some(&content));

    adw::NavigationPage::builder()
        .title("Results")
        .tag("results")
        .child(&scrolled)
        .build()
}
