use crate::ui::db_browser::DbBrowserMsg;
use crate::ui::home_page::HomePageMsg;
use crate::ui::jlc_downloader::JlcDownloaderMsg;
use crate::ui::part_viewer::PartViewerMsg;

#[allow(unused_imports)]
use super::db_browser;
#[allow(unused_imports)]
use super::home_page;
#[allow(unused_imports)]
use super::jlc_downloader;
#[allow(unused_imports)]
use super::part_viewer;
use iced::Theme;
#[allow(unused_imports)]
use iced::widget as w;
use iced::widget::button;
use iced::widget::button::Status;
use iced::widget::button::Style;
use iced::widget::row;
#[allow(unused_imports)]
use iced::{
    Element, Length,
    alignment::{Horizontal, Vertical},
    widget::{Column, Container, Text, column},
};
use std::fmt::Display;

#[allow(dead_code)]
struct MainWindow {
    title: String,
    theme: iced::Theme,
    curr_tab: TabId,
    home_page: crate::ui::home_page::HomePage,
    jlc_downloader: crate::ui::jlc_downloader::JlcDownloader,
    db_browser: crate::ui::db_browser::DbBrowser,
    part_viewer: crate::ui::part_viewer::PartViewer,
}
impl Default for MainWindow {
    fn default() -> Self {
        Self {
            title: "HardwareToolkit".into(),
            theme: Default::default(),
            curr_tab: Default::default(),
            home_page: Default::default(),
            jlc_downloader: Default::default(),
            db_browser: Default::default(),
            part_viewer: Default::default(),
        }
    }
}

pub fn main_window() {
    let _ = iced::application(MainWindow::default, MainWindow::update, MainWindow::view)
        .default_font(iced::Font::with_name("微软雅黑"))
        .title(MainWindow::title)
        .run();
}

#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum MainWindowMsg {
    ThemeChanged(iced::Theme),
    TitleChanged(String),
    TabSelected(TabId),
    HomePage(HomePageMsg),
    JlcDownloader(JlcDownloaderMsg),
    DbBrowser(DbBrowserMsg),
    PartViewer(PartViewerMsg),
    Nothing,
}

#[allow(dead_code)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub(crate) enum TabId {
    #[default]
    HomePage,
    JlcDownloader,
    DbBrowser,
    PartViewer,
}
impl Display for TabId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                TabId::HomePage => "主页",
                TabId::JlcDownloader => "3D模型下载",
                TabId::DbBrowser => "数据库浏览",
                TabId::PartViewer => "元件查看",
            }
        )
    }
}
fn btn_active_style(_theme: &Theme, _status: Status) -> Style {
    let mut s = Style::default();
    let bgcolor = iced::Color::from_rgba8(30, 200, 200, 1.0);
    s.background = Some(iced::Background::Color(bgcolor));
    s
}
fn btn_deactive_style(_theme: &Theme, _status: Status) -> Style {
    let mut s = Style::default();
    let bgcolor = iced::Color::from_rgba8(220, 200, 100, 1.0);
    s.background = Some(iced::Background::Color(bgcolor));
    s
}
impl MainWindow {
    fn title(&self) -> String {
        self.title.clone()
    }
    fn create_tab_btn(&self, tab: TabId) -> Element<'_, MainWindowMsg> {
        let bstyle = if self.curr_tab == tab {
            btn_active_style
        } else {
            btn_deactive_style
        };
        let txt = format!("{tab}");

        let txt = iced::widget::text(txt);
        let btn = button(txt).style(bstyle);
        btn.on_press(MainWindowMsg::TabSelected(tab.clone())).into()
    }
    fn update(&mut self, msg: MainWindowMsg) {
        println!("Process the msg: {msg:?}");
        match msg {
            MainWindowMsg::ThemeChanged(theme) => {
                self.theme = theme;
            }
            MainWindowMsg::TitleChanged(title) => {
                self.title = title;
            }
            MainWindowMsg::Nothing => {
                println!("Nothing");
                iced::debug::time("Test".to_owned());
            }
            MainWindowMsg::TabSelected(i) => {
                self.curr_tab = i;
            }
            MainWindowMsg::HomePage(msg) => {
                println!("update HomePage");
                self.home_page.update(msg);
            }
            MainWindowMsg::JlcDownloader(msg) => {
                self.jlc_downloader.update(msg);
            }
            MainWindowMsg::DbBrowser(msg) => {
                self.db_browser.update(msg);
            }
            MainWindowMsg::PartViewer(part_viewer_msg) => {
                self.part_viewer.update(part_viewer_msg);
            }
        }
    }
    fn view(&self) -> Element<'_, MainWindowMsg> {
        let h = row![
            self.create_tab_btn(TabId::HomePage),
            self.create_tab_btn(TabId::JlcDownloader),
            self.create_tab_btn(TabId::DbBrowser),
            self.create_tab_btn(TabId::PartViewer)
        ];
        let v = match self.curr_tab {
            TabId::HomePage => self.home_page.view(),
            TabId::JlcDownloader => self.jlc_downloader.view(),
            TabId::DbBrowser => self.db_browser.view(),
            TabId::PartViewer => self.part_viewer.view(),
        };
        // let content = iced::widget::Button::new("Click").on_press(MainWindowMsg::Nothing);
        column![h, v].into()
    }
}

pub trait TabContent {
    type TabMessage;
    fn update(&self, msg: Self::TabMessage);
    fn view(&self) -> Element<'_, MainWindowMsg> {
        iced::widget::Container::new(self.content())
            .width(Length::Fill)
            .height(Length::Fill)
            .align_x(Horizontal::Left)
            .align_y(Vertical::Top)
            .padding(16.0)
            .into()
    }
    fn content(&self) -> Element<'_, MainWindowMsg>;
}
