use colored::Colorize;
use regex::Regex;

pub fn parse_gemtext(page_content: String) -> (String, Vec<String>) {
	let mut parsed_page_content: String = "".to_string();
	let mut hyperlink_number_counter: u64 = 0;
	let mut links: Vec<String> = Vec::new();
        let mut preformatted_code_toggle = false;

        // Regex patterns
        let preformatted_text_regex = Regex::new(r"^```(.*)").unwrap();
        let header1_regex = Regex::new(r"^# (.*)").unwrap();
        let header2_regex = Regex::new(r"^## (.*)").unwrap();
        let header3_regex = Regex::new(r"^### (.*)").unwrap();
        let block_quotes_regex = Regex::new(r"^>(.*)").unwrap();
        let unordered_list_regex = Regex::new(r"^([ \t]+|^)(\*).(.*)").unwrap();
        let hyperlink_regex = Regex::new(r"=>\s(\S*)\s(.*)").unwrap();
        let quick_hyperlink_regex = Regex::new(r"=>\s(.*)").unwrap();
	
        for line in page_content.lines() {
            let mut parsed_line: String = line.to_string();
            let mut remove_line = false;

            // preformatted text
            parsed_line = preformatted_text_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                // Flip the toggle
                preformatted_code_toggle = ! preformatted_code_toggle; 

                if caps[1] == *""
                {
                    remove_line = true;
                }
                
                // Remove the ```
                format!("{}", &caps[1].magenta())
            }).to_string();

            if preformatted_code_toggle == false 
            {
                // Headers
                parsed_line = header1_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                    format!("{}", &caps[1].blue().bold().underline())
                }).to_string();
                parsed_line = header2_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                    format!("{}", &caps[1].blue().bold())
                }).to_string();
                parsed_line = header3_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                    format!("{}", &caps[1].bold())
                }).to_string();

                // Block quotes
                parsed_line = block_quotes_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                    format!("   | {}", &caps[1].red())
                }).to_string();

                // Unordered list ([    ]+|^)- (.*)
                parsed_line = unordered_list_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                    format!("{} • {}", &caps[1], &caps[3])
                }).to_string();

                // HyperLink
                parsed_line = hyperlink_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                    // Check if the character before the link is not '!'
                    let result = format!("[{}] {}", hyperlink_number_counter, &caps[2].blue().underline());
                    let url = caps[1].to_string();
                    links.push(url);
                    hyperlink_number_counter += 1;
                    result
                }).to_string();
                
                parsed_line = quick_hyperlink_regex.replace_all(&parsed_line, |caps: &regex::Captures| {
                    hyperlink_number_counter += 1;
                    let url = caps[1].to_string();
                    links.push(url);
                    format!("[{}] {}", hyperlink_number_counter, &caps[1].blue().underline())
                }).to_string();
            }
            else if preformatted_code_toggle == true 
            {
                parsed_line = parsed_line.magenta().to_string();
            }

            if remove_line == false
            {
                parsed_page_content+=&(parsed_line + "\n");
            }
        }


    return (parsed_page_content, links);
}
