use iced::theme::{self, Theme};
use iced::widget::{button, column, row, text};
use iced::widget::{text_input, TextInput};
use iced::{Alignment, Color, Element, Length, Sandbox, Settings};

mod my_theme;
use my_theme::{get_theme, FromHexColor, OneDark};

mod search_bar;
use search_bar::*;

mod message;
use message::*;

pub fn main() -> iced::Result {
    Counter::run(Settings::default())
}

struct AppSettings {
    entries_per_page: u32,
}

struct Counter {
    theme: Theme,
    value: i32,
    input_val: String,
    current_tab: Tab,
    app_settings: AppSettings,
}

impl Sandbox for Counter {
    type Message = Message;

    fn new() -> Self {
        Self {
            value: 0,
            input_val: String::from("0"),
            theme: get_theme(),
            current_tab: Tab::Content,
            app_settings: AppSettings {
                entries_per_page: 10,
            },
        }
    }

    fn title(&self) -> String {
        String::from("Counter - Iced")
    }

    fn update(&mut self, message: Message) {
        match message {
            Message::InputChanged(value) => {
                self.input_val = value.clone();
                if let Ok(value) = value.parse::<i32>() {
                    self.value = value;
                }
            }
            Message::SwitchTab(tab) => {
                self.current_tab = tab;
            }
            _ => {}
        }
    }

    fn view(&self) -> Element<Message> {
        column![
            row![text::Text::new("TFTool").size(50)],
            row![tabs(&self)],
            match self.current_tab {
                Tab::Content => search_bar(&self),
                Tab::Settings => text::Text::new("Settings").into(),
            }
        ]
        .width(Length::Fill)
        .padding([55, 0])
        .align_items(Alignment::Center)
        .into()
        // row![TextInput::new("Search", &self.input_val).on_input(Message::InputChanged)].into()
    }

    fn theme(&self) -> Theme {
        self.theme.clone()
    }
}
