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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use crate::utils;
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use std::path::PathBuf;
use tui::text::Line;

#[derive(Debug, Clone, PartialEq)]
pub enum AppMode {
    Normal,
    Search,
}

#[derive(Debug, Clone)]
pub enum AppState<'a> {
    Init,
    Initialized {
        paths: Vec<PathBuf>,
        path: String,
        selected_index: usize,
        term_size: Option<TermSize>,
        current_image: Option<Vec<Line<'a>>>,
        current_image_info: Option<ImageInfo>,
        search_term: String,
        app_mode: AppMode,
    },
}

#[derive(Debug, Clone)]
pub struct TermSize {
    pub width: u32,
    pub height: u32,
}

#[derive(Debug, Clone)]
pub struct ImageInfo {
    pub name: String,
    pub size: u64,
    pub dimensions: (u32, u32),
}

impl<'a> AppState<'a> {
    pub fn initialized(path: &str) -> Self {
        let paths = utils::get_image_paths(path);
        let selected_index = 0;
        let current_image = None;
        let term_size = None;
        let current_image_info = None;
        let search_term = "".to_string();
        let app_mode = AppMode::Normal;
        Self::Initialized {
            paths,
            path: path.to_string(),
            selected_index,
            term_size,
            current_image,
            current_image_info,
            search_term,
            app_mode,
        }
    }

    pub fn is_initialized(&self) -> bool {
        matches!(self, &Self::Initialized { .. })
    }

    pub fn get_paths(&self) -> Vec<PathBuf> {
        if let Self::Initialized { paths, .. } = self {
            paths.clone()
        } else {
            vec![]
        }
    }

    pub fn filter_paths(&mut self) {
        if let Self::Initialized {
            paths: self_paths,
            path,
            search_term,
            ..
        } = self
        {
            let matcher = SkimMatcherV2::default();

            // TODO: Load the image paths only once and filter it there.
            let paths = utils::get_image_paths(path)
                .into_iter()
                .filter(|path| {
                    let file_name = path
                        .file_name()
                        .unwrap()
                        .to_os_string()
                        .into_string()
                        .unwrap();
                    match matcher.fuzzy_match(&file_name, search_term) {
                        Some(_) => true,
                        None => false,
                    }
                })
                .collect();
            *self_paths = paths;
        }
    }

    pub fn get_path(&self, index: usize) -> Option<PathBuf> {
        if let Self::Initialized { paths, .. } = self {
            if paths.is_empty() {
                None
            } else {
                Some(paths[index].clone())
            }
        } else {
            None
        }
    }

    pub fn increment_index(&mut self) {
        if let Self::Initialized {
            selected_index,
            paths: images,
            ..
        } = self
        {
            *selected_index += 1;
            if *selected_index >= images.len() {
                *selected_index = 0;
            }
        }
    }

    pub fn decrement_index(&mut self) {
        if let Self::Initialized {
            selected_index,
            paths: images,
            ..
        } = self
        {
            if images.is_empty() {
                return;
            }

            if *selected_index == 0 {
                *selected_index = images.len();
            }
            *selected_index -= 1;
        }
    }

    pub fn get_index(&self) -> Option<usize> {
        if let Self::Initialized { selected_index, .. } = self {
            Some(*selected_index)
        } else {
            None
        }
    }

    pub fn set_term_size(&mut self, width: u32, height: u32) {
        if let Self::Initialized { term_size, .. } = self {
            *term_size = Some(TermSize { width, height });
        }
    }

    pub fn get_term_size(&self) -> Option<TermSize> {
        if let Self::Initialized { term_size, .. } = self {
            term_size.clone()
        } else {
            None
        }
    }

    pub fn set_current_image(&mut self, img: Vec<Line<'a>>) {
        if let Self::Initialized { current_image, .. } = self {
            *current_image = Some(img);
        }
    }

    pub fn get_current_image(&self) -> Option<Vec<Line<'a>>> {
        if let Self::Initialized { current_image, .. } = self {
            current_image.clone()
        } else {
            None
        }
    }

    pub fn set_current_image_info(&mut self, image_info: ImageInfo) {
        if let Self::Initialized {
            current_image_info, ..
        } = self
        {
            *current_image_info = Some(image_info);
        }
    }

    pub fn get_current_image_info(&self) -> Option<ImageInfo> {
        if let Self::Initialized {
            current_image_info, ..
        } = self
        {
            current_image_info.clone()
        } else {
            None
        }
    }

    pub fn get_search_term(&self) -> &str {
        let Self::Initialized { search_term, .. } = self else {
            return "";
        };
        search_term.as_ref()
    }

    pub fn set_search_term(&mut self, arg: String) {
        if let Self::Initialized { search_term, .. } = self {
            *search_term = arg;
        }
    }

    pub fn set_app_mode(&mut self, mode: AppMode) {
        if let Self::Initialized { app_mode, .. } = self {
            *app_mode = mode;
        }
    }
    pub fn get_app_mode(&self) -> AppMode {
        let Self::Initialized { app_mode, .. } = self else {
            return AppMode::Normal;
        };
        app_mode.clone()
    }
}

impl<'a> Default for AppState<'a> {
    fn default() -> Self {
        Self::Init
    }
}