use std::io::{Error, ErrorKind};
use std::ops::Index;
use std::{collections::HashMap, fs::read_to_string};

fn main() {
    // Read the input file
    let input = read_to_string("input_day_3.txt")
        .expect("Failed to read the input file.");
//     let input = 
// r#"467..114..
// ...*......
// ..35..633.
// ......#...
// 617*......
// .....+.58.
// ..592.....
// ......755.
// ...$.*....
// .664.598.."#.to_string();
    part_1(input);

}
#[derive(Debug)]
struct INDEXER {
    linenumber : usize,
    lineindex : Vec<usize>
}
#[derive(Debug)]
struct ListOfIndex {
    indexlist : Vec<INDEXER>
}

fn part_1(input :String) {
    // create the index
    let listofindex = createindex(input.clone());
    let mut total = 0;
    // go over all entries found in the index
    for index in listofindex.indexlist {
        // get the currentline number
        let currentline = index.linenumber;


        for charindex in index.lineindex {
            total += searcher(input.clone(), currentline -2, charindex);
           // dbg!(&total);
            total += searcher(input.clone(), currentline -1, charindex);
            //dbg!(&total);
            total += searcher(input.clone(), currentline, charindex);
            dbg!(&total);

        }
        
    }
    println!("ANSWER: {total}");
}

fn createindex(input :String) -> ListOfIndex {
    let mut listofindex = ListOfIndex {
        indexlist : vec![]
    };

    let mut currentline = 0;
    for line in input.lines() {
        currentline += 1;
        let mut currentchar = 0;
        let mut currentindex = INDEXER {
            linenumber : currentline,
            lineindex : vec![]
        };


       // dbg!(&currentline);
        'charloop: for char in line.chars() {
            currentchar += 1;
           // dbg!(&currentchar);
            if char == '.' {
                continue 'charloop;
            }
            if char.is_numeric() {
                continue 'charloop;
            }
            currentindex.lineindex.push(currentchar);
        }
        listofindex.indexlist.push(currentindex);

    }
   // dbg!(&listofindex);
    listofindex
}

fn searcher(input :String, lineneeded :usize, charindex :usize) -> u32 {
    // Get the current line
    let line = input.lines().nth(lineneeded).unwrap();
    // Get a vector of the characters in the line
    let chars :Vec<char> = line.chars().collect();

    // Check if there is a number on the right place
    if chars.index(charindex -2).is_numeric() {
        walker(chars, charindex -2)
    }
    else if chars.index(charindex -1).is_numeric() {
        walker(chars, charindex -1)
    }
    else if chars.index(charindex).is_numeric() {
        walker(chars, charindex)
    } else {0}


}

fn walker(chars: Vec<char>, index :usize) -> u32 {
    let mut numberstring = String::new();
    numberstring.push(*chars.index(index));

    let mut backwalker = index -1;
    'backwalkloop: while chars.index(backwalker).is_numeric() {
        numberstring.insert(0, *chars.index(backwalker));
        if backwalker == 0{
            break 'backwalkloop;
        }
        backwalker -= 1;
    }
    let mut frontwalker = index +1;
    'frontwalkloop: while chars.index(frontwalker).is_numeric() {
        numberstring.push(*chars.index(frontwalker));
        if frontwalker == 139  {
            break 'frontwalkloop;
        }
        frontwalker += 1;
    }
    dbg!(&numberstring);
    numberstring.parse::<u32>().unwrap()
    

    
}