1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::board::{Board, Square};
use crate::board::enums::*;

pub struct RowIndexIterator<'a> {
    board: &'a Board,
    row_cursor: usize
}

impl<'a> RowIndexIterator<'a> {
    pub fn new(board: &'a Board) -> Self {
        Self { 
            board, 
            row_cursor: 0 
        }
    }
}

impl<'a> Iterator for RowIndexIterator<'a> {
    type Item = Vec<usize>;
    
    /// Get next item from the iterator
    fn next(&mut self) -> Option<Vec<usize>> {
        if self.row_cursor >= self.board.height {
            return None
        }
        let mut holder = Vec::with_capacity(self.board.width);

        let start_idx = self.board.cell_index(self.row_cursor, 0);
        for i in start_idx..start_idx + self.board.width {
            holder.push(i);
        }

        self.row_cursor += 1;
        Some(holder)
    }
}

pub struct RowSquareIterator<'a> {
    board: &'a Board,
    index_iter: RowIndexIterator<'a>,
}

impl<'a> RowSquareIterator<'a> {
    pub fn new(board: &'a Board) -> Self {
        Self { 
            board, 
            index_iter: RowIndexIterator::new(board)
        }
    }
}

impl<'a> Iterator for RowSquareIterator<'a> {
    type Item = Vec<Square>;
    
    /// Get next item from the iterator
    fn next(&mut self) -> Option<Vec<Square>> {

        match self.index_iter.next() {
            Some(x) => {
                let mut holder: Vec<Square> = Vec::with_capacity(self.board.width);
                for i in x {
                    holder.push(self.board.cell(i));
                }
                Some(holder)
            },
            None => None
        }
    }
}

pub struct PieceIterator<'a> {
    board: &'a Board,
    index_cursor: usize,
}

impl<'a> PieceIterator<'a> {
    pub fn new(board: &'a Board) -> Self {
        Self { 
            board, 
            index_cursor: 0
        }
    }
}

impl<'a> Iterator for PieceIterator<'a> {
    type Item = (usize, Square);
    
    /// Get next item from the iterator
    fn next(&mut self) -> Option<(usize, Square)> {

        while self.index_cursor < self.board.num_cells() - 1 {
            self.index_cursor += 1;
            match self.board.cell(self.index_cursor).state {
                SquareState::Empty | SquareState::Unplayable => continue,
                SquareState::Occupied => return Some((self.index_cursor, self.board.cell(self.index_cursor))),
            }
        }

        None
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::board::enums::SquareState;
    use crate::board::Piece;
    use wasm_bindgen_test::*;

    // use crate::log;

    wasm_bindgen_test_configure!(run_in_browser);

    #[wasm_bindgen_test]
    fn index_iterator() {
        let board = Board::new(2, 2, Team::Black);
        let iter = RowIndexIterator::new(&board);
        let collected: Vec<Vec<usize>> = iter.collect();
        assert_eq!(vec![
            vec![0, 1],
            vec![2, 3]
        ], collected);
    }

    #[wasm_bindgen_test]
    fn square_iterator() {
        let board = Board::new(2, 2, Team::Black);
        let iter = RowSquareIterator::new(&board);
        let collected: Vec<Vec<Square>> = iter.collect();
        assert_eq!(vec![
            vec![
                Square::new(SquareState::Unplayable, Option::None), 
                Square::new(SquareState::Empty, Option::None)
                ],
            vec![
                Square::new(SquareState::Empty, Option::None), 
                Square::new(SquareState::Unplayable, Option::None)
                ]
        ], collected);
    }

    #[wasm_bindgen_test]
    fn piece_iterator_one_piece() {
        let idx = 2;

        let mut board = Board::new(4, 4, Team::Black);
        board.set_cell(idx, Square::new(
            SquareState::Occupied, 
            Some(
                Piece::new(Team::White, Strength::Man)
            )
        ));

        let iter = PieceIterator::new(&board);
        let collected: Vec<(usize, Square)> = iter.collect();

        assert_eq!(collected.len(), 1);
        assert_eq!(collected[0], (idx, board.cell(idx)));
    }

    #[wasm_bindgen_test]
    fn piece_iterator_multiple_pieces() {

        let mut board = Board::new(4, 4, Team::Black);
        board.set_cell(2, Square::new(
            SquareState::Occupied, 
            Some(
                Piece::new(Team::White, Strength::Man)
            )
        ));

        board.set_cell(4, Square::new(
            SquareState::Occupied, 
            Some(
                Piece::new(Team::Black, Strength::Man)
            )
        ));

        board.set_cell(5, Square::new(
            SquareState::Occupied, 
            Some(
                Piece::new(Team::White, Strength::King)
            )
        ));

        let iter = PieceIterator::new(&board);
        let collected: Vec<(usize, Square)> = iter.collect();

        assert_eq!(collected.len(), 3);
        assert_eq!(collected[0], (2, board.cell(2)));
        assert_eq!(collected[1], (4, board.cell(4)));
        assert_eq!(collected[2], (5, board.cell(5)));
    }

}