use iced::{Length, alignment::Horizontal, widget::Column};

#[allow(unused_imports)]
use crate::ui::main_window::{MainWindowMsg, TabContent};

pub struct HomePage {
    step_dir: String,
    pub theme: iced::Theme,
}
impl Default for HomePage {
    fn default() -> Self {
        Self {
            step_dir: crate::utils::app_settings::get_step_dir().unwrap(),
            theme: Default::default(),
        }
    }
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum HomePageMsg {
    #[default]
    Nothing,
    CheckUpdate,
    OpenStepDir,
    ChooseStepDir,
}

impl TabContent for HomePage {
    fn content(&self) -> iced::Element<'_, MainWindowMsg> {
        let info = iced::widget::row![
            iced::widget::text("版本："),
            iced::widget::text(env!("CARGO_PKG_VERSION")),
            iced::widget::horizontal_space(),
            iced::widget::button("检查更新")
                .on_press(MainWindowMsg::HomePage(HomePageMsg::CheckUpdate)),
        ];
        let stp_path_set = iced::widget::row![
            iced::widget::text("Step下载路径："),
            iced::widget::text(self.step_dir.clone()),
            iced::widget::button("打开")
                .on_press(MainWindowMsg::HomePage(HomePageMsg::OpenStepDir)),
            iced::widget::button("浏览")
                .on_press(MainWindowMsg::HomePage(HomePageMsg::ChooseStepDir))
        ]
        .spacing(16.0);
        let theme = iced::widget::row![
            iced::widget::text("主题选择："),
            iced::widget::pick_list(
                iced::Theme::ALL,
                Some(&self.theme),
                MainWindowMsg::ThemeChanged
            )
            .width(480),
        ]
        .spacing(16.0);
        Column::new()
            .align_x(Horizontal::Left)
            .width(Length::Fill)
            .height(Length::Fill)
            .push(info)
            .push(stp_path_set)
            .push(theme)
            .spacing(5.0)
            .into()
    }

    type TabMessage = HomePageMsg;

    fn update(&self, msg: Self::TabMessage) {
        match msg {
            HomePageMsg::Nothing => {
                println!("This way ok.");
            }
            _ => {
                println!("Is the message you should process ?  =====>>  {msg:?}");
            }
        }
    }
}
