use iced::ContentFit;
use iced::widget::image::Handle;
use iced::widget::{button, column, row, stack, text, text_input};
use iced::{Element, Event, Length, Task as Command, Theme, event, widget::image};
use iced_layershell::Application;
use iced_layershell::reexport::{Anchor, Layer};
use iced_layershell::settings::{LayerShellSettings, Settings, StartMode};
use iced_layershell::to_layer_message;

pub fn main() -> Result<(), iced_layershell::Error> {
    let binded_output_name = std::env::args().nth(1);
    let start_mode = match binded_output_name {
        Some(output) => StartMode::TargetScreen(output),
        None => StartMode::Active,
    };

    Counter::run(Settings {
        layer_settings: LayerShellSettings {
            size: Some((0, 0)),
            exclusive_zone: 0,
            anchor: Anchor::all(),
            start_mode,
            layer: Layer::Background,
            ..Default::default()
        },
        ..Default::default()
    })
}

struct Counter {}

// Because new iced delete the custom command, so now we make a macro crate to generate
// the Command
#[to_layer_message]
#[derive(Debug, Clone)]
enum Message {
    IcedEvent(Event),
}

impl Application for Counter {
    type Message = Message;
    type Flags = ();
    type Theme = Theme;
    type Executor = iced::executor::Default;

    fn new(_flags: ()) -> (Self, Command<Message>) {
        (Self {}, Command::none())
    }

    fn namespace(&self) -> String {
        String::from("project honeycomb home")
    }

    fn subscription(&self) -> iced::Subscription<Self::Message> {
        event::listen().map(Message::IcedEvent)
    }

    fn update(&mut self, message: Message) -> Command<Message> {
        match message {
            _ => Command::none(),
        }
    }

    fn view(&self) -> Element<Message> {
        let background = image::Image::new(Handle::from_bytes(include_bytes!(
            "res/default_background.png"
        ) as &[u8]))
        .content_fit(ContentFit::Cover);

        stack!(background).push(text("hi :)")).into()
    }
}
