use crate::html::*;
use build_html::{self, Container, ContainerType, Html, HtmlContainer};
use file_service::{FileHandle, FileInfo, ReadFileError};

pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
    build_html::HtmlPage::new()
        .with_title("Sign In")
        .with_stylesheet("/css")
        .with_container(
            Container::new(ContainerType::Div)
                .with_attributes([("class", "authentication-page")])
                .with_container(auth_form()),
        )
}

fn auth_form() -> Container {
    Container::default()
        .with_attributes([("class", "card authentication-form")])
        .with_html(
            Form::new()
                .with_path("/auth")
                .with_method("post")
                .with_container(
                    Container::new(ContainerType::Div)
                        .with_html(
                            Input::new("password", "password")
                                .with_id("for-token-input")
                                .with_attributes([
                                    ("size", "50"),
                                    ("class", "authentication-form__input"),
                                ]),
                        )
                        .with_html(
                            Button::new("Sign In")
                                .with_attributes([("class", "authentication-form__button")]),
                        ),
                ),
        )
}

pub fn gallery(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::HtmlPage {
    let mut page = build_html::HtmlPage::new()
        .with_title("Gallery")
        .with_stylesheet("/css")
        .with_container(
            Container::new(ContainerType::Div)
                .with_attributes([("class", "gallery-page")])
                .with_header(1, "Gallery")
                .with_html(upload_form()),
        );

    let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]);
    for handle in handles {
        let container = match handle {
            Ok(ref handle) => thumbnail(&handle.info),
            Err(err) => Container::new(ContainerType::Div)
                .with_attributes(vec![("class", "file")])
                .with_paragraph(format!("{:?}", err)),
        };
        gallery.add_container(container);
    }
    page.add_container(gallery);
    page
}

pub fn upload_form() -> Form {
    Form::new()
        .with_path("/upload")
        .with_method("post")
        .with_encoding("multipart/form-data")
        .with_container(
            Container::new(ContainerType::Div)
                .with_attributes([("class", "card upload-form")])
                .with_html(Input::new("file", "file").with_attributes([
                    ("id", "for-selector-input"),
                    ("placeholder", "select file"),
                    ("class", "upload-form__selector"),
                ]))
                .with_html(
                    Button::new("Upload file")
                        .with_attributes([("class", "upload-form__button")])
                        .with_type("submit"),
                ),
        )
}

pub fn thumbnail(info: &FileInfo) -> Container {
    Container::new(ContainerType::Div)
        .with_attributes(vec![("class", "card thumbnail")])
        .with_html(
            Container::new(ContainerType::Div).with_link(
                format!("/{}", *info.id),
                Container::default()
                    .with_attributes([("class", "thumbnail")])
                    .with_image(format!("{}/tn", *info.id), "test data")
                    .to_html_string(),
            ),
        )
        .with_html(
            Container::new(ContainerType::Div)
                .with_html(
                    Container::new(ContainerType::UnorderedList)
                        .with_attributes(vec![("class", "thumbnail__metadata")])
                        .with_html(info.name.clone())
                        .with_html(format!("{}", info.created.format("%Y-%m-%d"))),
                )
                .with_html(
                    Form::new()
                        .with_path(&format!("/delete/{}", *info.id))
                        .with_method("post")
                        .with_html(Button::new("Delete")),
                ),
        )
}
