use std::{cell::Cell, collections::HashSet};

use orbtk::prelude::*;
use orbtk::theme::DEFAULT_THEME_CSS;
use orbtk::*;

static DARK_EXT: &'static str = include_str!("../resources/stylesheets/advotracker-dark.css");

//#[cfg(feature = "light-theme")]
static LIGHT_EXT: &'static str = include_str!("../resources/stylesheets/advotracker-light.css");

static CSS_EXT: &'static str = include_str!("../resources/stylesheets/grid.css");

//#[cfg(not(feature = "light-theme"))]
//fn get_theme() -> ThemeValue {
//    ThemeValue::create_from_css(DEFAULT_THEME_CSS)
//        .extension_css(CSS_EXT)
//        .build()
//}

fn get_theme() -> ThemeValue {
    ThemeValue::create()
        //.extension_css(DARK_EXT)
        .extension_css(LIGHT_EXT)
        //.extension_css(CSS_EXT)
        .build()
}

#[derive(Debug, Copy, Clone)]
enum Action {
    IncrementCounter,
}

pub struct MainViewState {
    counter: Cell<i32>,
    action: Cell<Option<Action>>,
}

impl MainViewState {
    fn action(&self, action: impl Into<Option<Action>>) {
        self.action.set(action.into());
    }
}

impl Default for MainViewState {
    fn default() -> Self {
        MainViewState {
            counter: Cell::new(0),
            action: Cell::new(None),
        }
    }
}

impl State for MainViewState {
    fn update(&self, ctx: &mut Context<'_>) {
        if let Some(action) = self.action.get() {
            match action {
                Action::IncrementCounter => {
                    self.counter.set(self.counter.get() + 1);
                    ctx.widget().set(
                        "result",
                        String16::from(format!("Button count: {}", self.counter.get())),
                    );
                }
            }

            self.action.set(None);
        }
    }
}

fn create_header(ctx: &mut BuildContext, text: &str) -> Entity {
    TextBlock::create()
        .text(text)
        .selector(Selector::new().with("text-block").class("h1"))
        .build(ctx)
}

widget!(
    MainView<MainViewState> {
        selected_indices: SelectedIndices,
        text_harm: String16,
        result: String16
    }
);

impl Template for MainView {
    fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
        let state = self.clone_state();

        self.name("MainView").child(
            //.result("Button count: 0")
            //.selected_indices(HashSet::new())
            /*
            Grid::create()
                .margin(8.0)
                .rows(
                    Rows::create()
                        // define two rows
                        .row("*")
                        .row("*")
                        .build(),
                )
                .child(
                    // Container 0
                    Container::create()
                        .padding(8.0)
                        .selector(Selector::from("container").class("header"))
                        .attach(Grid::row(0))
                        .child(
             */
            Grid::create()
                .selector("lynch")
                .margin(8.0)
                .columns(
                    Columns::create()
                        // define three columns
                        .column("auto")
                        .column(24.0)
                        .column("auto")
                        .build(),
                )
                .child(
                    Stack::create()
                        // Column 0
                        .attach(Grid::column(0))
                        .child(create_header(ctx, "Buttons"))
                        .child(
                            Button::create()
                                .text("Button")
                                .selector(Selector::new().with("button").class("primary"))
                                .margin((0.0, 8.0, 0.0, 0.0))
                                .icon(material_font_icons::CHECK_FONT_ICON)
                                .attach(Grid::column(0))
                                .attach(Grid::row(1))
                                .on_mouse_move(move |_| {
                                    println!("Mouse moved over Button!");
                                    true
                                })
                                .on_click(move |_| {
                                    println!("Mouse clicked Buttonmoved over Button!");
                                    state.action(Action::IncrementCounter);
                                    true
                                })
                                .build(ctx),
                        )
                        .child(
                            Switch::create()
                                .selector("bluebayoux")
                                .margin((0.0, 8.0, 0.0, 0.0))
                                .attach(Grid::column(0))
                                .attach(Grid::row(2))
                                .build(ctx),
                        )
                        .build(ctx),
                )
                .child(
                    Stack::create()
                        // Column 2
                        .attach(Grid::column(2))
                        .child(create_header(ctx, "Text"))
                        .child(
                            TextBlock::create()
                                .selector(Selector::new().class("body"))
                                .text(("result", id))
                                .margin((0.0, 8.0, 0.0, 0.0))
                                .attach(Grid::column(2))
                                .attach(Grid::row(1))
                                .build(ctx),
                        )
                        .child(
                            TextBox::create()
                                .water_mark("Harm...")
                                .text(("text_harm", id))
                                .margin((0.0, 8.0, 0.0, 0.0))
                                .attach(Grid::column(2))
                                .attach(Grid::row(2))
                                .build(ctx),
                        )
                        .build(ctx),
                )
                .build(ctx),
        )
        /*.child(
            // Container 1
                Container::create()
                .selector(Selector::from("container").class("content"))
                .padding(8.0)
                .attach(Grid::row(1))
                .child(
                ImageWidget::create()
                .image("resources/images/orbtk-space.png")
                .visibility("Hidden")
                .margin(8.0)
                .vertical_alignment("Center")
                .horizontal_alignment("Center")
                .size(600.0, 600.0)
                .clip(true)
                .build(ctx),
        )
                .build(ctx),
        )
                .build(ctx),
        )
                    .build(ctx),
        )
             */
    }
}

fn main() {
    // use this only if you want to run it as web application.
    orbtk::initialize();

    let mut application = Application::default();
    application
        .create_window()
        .bounds((100.0, 100.0, 420.0, 730.0))
        .title("OrbTk - advotracker test GUI")
        .resizeable(true)
        .root(MainView::create())
        .build();
    application.run();

    /*
    Application::new()
        .window(|ctx| {
            Window::create()
                .title("OrbTk - advotracker test GUI")
                .position((100.0, 100.0))
                //.size(600.0, 800.0)
                .size(640.0, 480.0)
                .resizeable(true)
                //.theme(get_theme())
                .child(MainView::create().build(ctx))
                .build(ctx)
        })
        .run();
     */
}
