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

#[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(&mut self, msg: Self::TabMessage) -> Task<MainWindowMsg> {
        match msg {
            HomePageMsg::Nothing => {
                info!("This way ok.");
            }
            HomePageMsg::OpenStepDir => {
                info!("To open the dir.");
                let _ = std::process::Command::new(r"explorer.exe")
                    .arg(self.step_dir.as_str())
                    .output()
                    .unwrap()
                    .stdout;
            }
            HomePageMsg::ChooseStepDir => {
                info!("To choose the step dir.");
                if let Some(path) = rfd::FileDialog::new().pick_folder() {
                    let path = path.to_str();
                    if let Some(path) = path {
                        self.step_dir = path.to_string();
                        let _ = crate::utils::app_settings::set_step_dir(path.to_string().as_str());
                    }
                }
            }
            HomePageMsg::CheckUpdate => {
                info!("To check update.");
            }
        }
        Task::none()
    }
}
