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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::path::PathBuf;
use std::sync::Arc;

use clap::Args;
use color_eyre::eyre::Result;
use cursive::event::Key;
use cursive::theme::{BorderStyle, Color::TerminalDefault, Palette, PaletteColor::*, Theme};
use cursive::view::Nameable;
use cursive::views::{
    Dialog, DummyView, HideableView, LinearLayout, NamedView, ScrollView, SelectView, TextView,
};
use cursive::{Cursive, CursiveRunnable, With};
use fluent_templates::Loader;
use futures::executor;
use novel_api::{ChapterInfo, CiweimaoClient, Client, ContentInfo, SfacgClient};
use url::Url;

use crate::cmd::{Convert, Source};
use crate::{utils, LANG_ID, LOCALES};

#[must_use]
#[derive(Args)]
#[command(arg_required_else_help = true,
    about = LOCALES.lookup(&LANG_ID, "read_command").unwrap())]
pub struct Read {
    #[arg(help = LOCALES.lookup(&LANG_ID, "novel_id").unwrap())]
    pub novel_id: u32,

    #[arg(short, long,
        help = LOCALES.lookup(&LANG_ID, "source").unwrap())]
    pub source: Source,

    #[arg(short, long, value_enum, value_delimiter = ',',
        help = LOCALES.lookup(&LANG_ID, "converts").unwrap())]
    pub converts: Vec<Convert>,

    #[arg(long, default_value_t = false,
        help = LOCALES.lookup(&LANG_ID, "ignore_keyring").unwrap())]
    pub ignore_keyring: bool,

    #[arg(long, num_args = 0..=1, default_missing_value = super::DEFAULT_PROXY,
        help = LOCALES.lookup(&LANG_ID, "proxy").unwrap())]
    pub proxy: Option<Url>,

    #[arg(long, default_value_t = false,
        help = LOCALES.lookup(&LANG_ID, "no_proxy").unwrap())]
    pub no_proxy: bool,

    #[arg(long, num_args = 0..=1, default_missing_value = super::default_cert_path(),
        help = super::cert_help_msg())]
    pub cert: Option<PathBuf>,
}

pub async fn execute(config: Read) -> Result<()> {
    match config.source {
        Source::Sfacg => {
            let mut client = SfacgClient::new().await?;
            super::set_options(&mut client, &config.proxy, &config.no_proxy, &config.cert);
            do_execute(client, config).await?
        }
        Source::Ciweimao => {
            let mut client = CiweimaoClient::new().await?;
            super::set_options(&mut client, &config.proxy, &config.no_proxy, &config.cert);
            do_execute(client, config).await?
        }
    }

    Ok(())
}

type ScrollableTextView = ScrollView<NamedView<TextView>>;
type ScrollableSelectView = ScrollView<NamedView<SelectView<Option<ChapterInfo>>>>;
type HideableSelectView = HideableView<NamedView<ScrollableSelectView>>;

async fn do_execute<T>(client: T, config: Read) -> Result<()>
where
    T: Client + Send + Sync + 'static,
{
    utils::login(&client, &config.source, config.ignore_keyring).await?;

    let client = Arc::new(client);
    super::handle_ctrl_c(&client);

    let mut siv = cursive::default();
    set_theme(&mut siv);
    set_shortcut_keys(&mut siv);

    let mut select = SelectView::new();
    let select_width = (viuer::terminal_size().0 / 3) as usize;

    for volume in client.volume_infos(config.novel_id).await? {
        let volume_title = utils::convert_str(volume.title, &config.converts)?;
        select.add_item(
            console::truncate_str(&volume_title, select_width, "..."),
            None,
        );

        for chapter in volume.chapter_infos {
            let chapter_title = format!(
                "  {}",
                utils::convert_str(&chapter.title, &config.converts)?
            );

            select.add_item(
                console::truncate_str(&chapter_title, select_width, "..."),
                Some(chapter),
            );
        }
    }

    let client_copy = Arc::clone(&client);
    let convert_copy = config.converts.clone();
    select.set_on_select(move |s, info| {
        if info.is_some() {
            let info = info.as_ref().unwrap();

            // TODO Distinguish between chapters not purchased and inaccessible
            if info.can_download() {
                if let Ok(content) = download(&client_copy, info, &convert_copy) {
                    s.call_on_name("scrollable_text", |view: &mut ScrollableTextView| {
                        view.scroll_to_top();
                    });

                    s.call_on_name("text", |view: &mut TextView| {
                        view.set_content(content);
                    });
                } else {
                    s.add_layer(create_dialog(
                        LOCALES.lookup(&LANG_ID, "download_failed_msg").unwrap(),
                    ));
                }
            } else {
                s.add_layer(create_dialog(
                    LOCALES.lookup(&LANG_ID, "inaccessible_msg").unwrap(),
                ));
            };
        }
    });

    siv.add_fullscreen_layer(
        LinearLayout::horizontal()
            .child(
                HideableView::new(
                    ScrollView::new(select.with_name("select")).with_name("scrollable_select"),
                )
                .with_name("hideable_select"),
            )
            .child(DummyView)
            .child(
                ScrollView::new(
                    TextView::new(introduction(&client, config.novel_id, &config.converts).await?)
                        .with_name("text"),
                )
                .with_name("scrollable_text"),
            ),
    );

    siv.run();

    Ok(())
}

fn set_theme(siv: &mut CursiveRunnable) {
    siv.set_theme(Theme {
        shadow: false,
        borders: BorderStyle::Simple,
        palette: Palette::retro().with(|palette| {
            palette[Background] = TerminalDefault;
            palette[Shadow] = TerminalDefault;
            palette[View] = TerminalDefault;
            palette[Primary] = TerminalDefault;
            palette[Secondary] = TerminalDefault;
            palette[Tertiary] = TerminalDefault;
            palette[TitlePrimary] = TerminalDefault;
            palette[TitleSecondary] = TerminalDefault;
            palette[Highlight] = TerminalDefault;
            palette[HighlightInactive] = TerminalDefault;
            palette[HighlightText] = TerminalDefault;
        }),
    });
}

fn set_shortcut_keys(siv: &mut CursiveRunnable) {
    siv.add_global_callback('q', Cursive::quit);
    siv.add_global_callback('h', |s| {
        s.call_on_name("hideable_select", |view: &mut HideableSelectView| {
            if view.is_visible() {
                view.hide();
            } else {
                view.unhide();
            }
        });
    });

    // TODO Handle the situation where a non-downloadable chapter is encountered
    siv.add_global_callback(Key::Left, |s| {
        let callback = s
            .call_on_name("select", |view: &mut SelectView<Option<ChapterInfo>>| {
                let mut callback = view.select_up(1);

                loop {
                    let id = view.selected_id().unwrap();
                    if id == 0 {
                        break;
                    }

                    let (_, info) = view.get_item(id).unwrap();
                    if info.is_none() {
                        callback = view.select_up(1);
                    } else {
                        break;
                    }
                }

                callback
            })
            .unwrap();
        callback(s);

        s.call_on_name("scrollable_select", |view: &mut ScrollableSelectView| {
            view.scroll_to_important_area();
        });
    });

    siv.add_global_callback(Key::Right, |s| {
        let callback = s
            .call_on_name("select", |view: &mut SelectView<Option<ChapterInfo>>| {
                let mut callback = view.select_down(1);

                loop {
                    let id = view.selected_id().unwrap();
                    if id == view.len() - 1 {
                        break;
                    }

                    let (_, info) = view.get_item(id).unwrap();
                    if info.is_none() {
                        callback = view.select_down(1);
                    } else {
                        break;
                    }
                }

                callback
            })
            .unwrap();
        callback(s);

        s.call_on_name("scrollable_select", |view: &mut ScrollableSelectView| {
            view.scroll_to_important_area();
        });
    });
}

fn download<T, E>(client: &Arc<T>, chapter_info: &ChapterInfo, converts: E) -> Result<String>
where
    T: Client + Send + Sync + 'static,
    E: AsRef<[Convert]>,
{
    let mut result = String::with_capacity(8192);
    result.push_str(&utils::convert_str(&chapter_info.title, &converts)?);
    result.push_str("\n\n");

    for info in executor::block_on(client.content_infos(chapter_info))? {
        if let ContentInfo::Text(text) = info {
            result.push_str(&utils::convert_str(&text, &converts)?);
            result.push_str("\n\n");
        } else if let ContentInfo::Image(url) = info {
            result.push_str(url.to_string().as_str());
            result.push_str("\n\n");
        } else {
            unreachable!("ContentInfo can only be Text or Image");
        }
    }

    Ok(result)
}

async fn introduction<T, E>(client: &Arc<T>, novel_id: u32, converts: E) -> Result<String>
where
    T: Client + Send + Sync + 'static,
    E: AsRef<[Convert]>,
{
    let novel_info = utils::novel_info(client, novel_id).await?;

    let mut introduction = String::with_capacity(4096);
    if let Some(lines) = novel_info.introduction {
        for line in lines {
            introduction.push_str(&utils::convert_str(&line, &converts)?);
            introduction.push_str("\n\n");
        }
    }

    Ok(introduction)
}

fn create_dialog<T>(msg: T) -> Dialog
where
    T: AsRef<str>,
{
    Dialog::text(msg.as_ref()).button("Ok", |s| {
        s.pop_layer();
    })
}