//! A module that handles the view for the search page in the `crabbysearch` frontend.

use maud::{html, Markup, PreEscaped};

use crate::{
    models::aggregation_models::SearchResults,
    templates::partials::{bar::bar, footer::footer, header::header},
};

/// A function that handles the html code for the search page view in the search engine frontend.
///
/// # Arguments
///
/// * `colorscheme` - It takes the colorscheme name as an argument.
/// * `theme` - It takes the theme name as an argument.
/// * `query` - It takes the current search query provided by the user as an argument.
/// * `search_results` - It takes the aggregated search results as an argument.
///
/// # Returns
///
/// It returns the compiled html markup code as a result.
pub fn search(query: &str, search_results: &SearchResults) -> Markup {
    html!(
        (header())
        main class="results"{
           (bar(query))
           .results_aggregated{
              @if !search_results.results.is_empty() {
                  @for result in search_results.results.iter(){
                      .result {
                         h1{a href=(result.url){(PreEscaped(&result.title))}}
                         small{(result.url)}
                         p{(PreEscaped(&result.description))}
                         .upstream_engines{
                            @for name in &result.engine {
                               span{(name)}
                            }
                         }
                      }
                  }
              }
              @else if search_results.no_engines_selected {
                 .result_engine_not_selected{
                    .description{
                       p{
                          "No results could be fetched for your search '{span class="user_query"{(query)}}'."
                       }
                       p class="description_paragraph"{"Dear user,"}
                       p class="description_paragraph"{
                          "No results could be retrieved from the upstream search engines as no
                          upstream search engines were selected from the settings page."
                       }
                    }
                    img src="./images/no_selection.png" alt="Image of a white cross inside a red circle";
                 }
              }
              @else {
                 .result_not_found {
                    p{"Your search - "{(query)}" - did not match any documents."}
                    p class="suggestions"{"Suggestions:"}
                    ul{
                       li{"Make sure that all words are spelled correctly."}
                       li{"Try different keywords."}
                       li{"Try more general keywords."}
                    }
                    img src="./images/no_results.gif" alt="Man fishing gif";
                 }
              }
            }
            .page_navigation {
               button type="button" onclick="navigate_backward()"{
                   (PreEscaped("&#8592;")) "previous"
               }
               button type="button" onclick="navigate_forward()"{"next" (PreEscaped("&#8594;"))}
            }
        }
        script src="static/index.js"{}
        script src="static/search_area_options.js"{}
        script src="static/pagination.js"{}
        script src="static/error_box.js"{}
        (footer())
    )
}
