use crate::{gui_handler::gui::iconizable::IconBuilderType, prelude::*};
use anyhow::Result;
use std::sync::{Arc, RwLock};

use self::fill_type::FillType;

pub mod button;
pub mod fill_type;
pub mod grid;
pub mod icon;
pub mod label;
pub mod multi_line_label;
pub mod multi_line_textfield;
pub mod progress_bar;
pub mod textfield;
pub mod ui_element;

pub mod traits;

mod callback_builder;
pub mod prelude;
mod wrapper;

pub(crate) fn set_background(
    gui_handler: &mut GuiHandler,
    visible: bool,
    framable: &Arc<Framable>,
    current: &RwLock<Option<FillType>>,
    new: impl Into<FillTypeInfo>,
) -> Result<()> {
    let mut current_background = current.write().unwrap();

    let fill_info = new.into();

    let update = match &*current_background {
        Some(bg) => *bg != fill_info,
        None => true,
    };

    if update {
        let fill_type = FillType::new(gui_handler, framable.clone(), fill_info)?;

        if framable.is_framed() {
            fill_type.update_frame(gui_handler)?;
        }

        if visible {
            fill_type.enable(gui_handler)?;
        }

        *current_background = Some(fill_type);
    }

    Ok(())
}