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
use std::fmt::Display;
use synd_auth::device_flow::{DeviceAccessTokenResponse, DeviceAuthorizationResponse};
use synd_feed::types::Category;

use crate::{
    application::{Direction, ListAction, RequestSequence},
    auth::AuthenticationProvider,
    client::{
        mutation::subscribe_feed::SubscribeFeedInput, payload,
        query::subscription::SubscriptionOutput,
    },
    types::Feed,
};

#[derive(Debug, Clone)]
pub enum Command {
    Quit,
    ResizeTerminal {
        columns: u16,
        rows: u16,
    },
    RenderThrobber,
    Idle,

    Authenticate,
    // Authenticate(AuthenticationProvider),
    MoveAuthenticationProvider(Direction),

    DeviceAuthorizationFlow {
        provider: AuthenticationProvider,
        device_authorization: DeviceAuthorizationResponse,
    },
    CompleteDevieAuthorizationFlow {
        provider: AuthenticationProvider,
        device_access_token: DeviceAccessTokenResponse,
    },

    MoveTabSelection(Direction),

    // Subscription
    MoveSubscribedFeed(Direction),
    MoveSubscribedFeedFirst,
    MoveSubscribedFeedLast,
    PromptFeedSubscription,
    PromptFeedEdition,
    PromptFeedUnsubscription,
    SubscribeFeed {
        input: SubscribeFeedInput,
    },
    UnsubscribeFeed {
        url: String,
    },
    CompleteSubscribeFeed {
        feed: Feed,
        request_seq: RequestSequence,
    },
    CompleteUnsubscribeFeed {
        url: String,
        request_seq: RequestSequence,
    },
    FetchSubscription {
        after: Option<String>,
        first: i64,
    },
    UpdateSubscriptionState {
        action: ListAction,
        subscription: SubscriptionOutput,
        request_seq: RequestSequence,
    },
    ReloadSubscription,
    OpenFeed,

    // Entries
    FetchEntries {
        after: Option<String>,
        first: i64,
    },
    UpdateEntriesState {
        action: ListAction,
        payload: payload::FetchEntriesPayload,
        request_seq: RequestSequence,
    },
    ReloadEntries,
    MoveEntry(Direction),
    MoveEntryFirst,
    MoveEntryLast,
    OpenEntry,

    // Filter
    MoveFilterRequirement(Direction),
    ActivateCategoryFilterling,
    DeactivateCategoryFiltering,
    ToggleFilterCategory {
        category: Category<'static>,
    },
    ActivateAllFilterCategories,
    DeactivateAllFilterCategories,

    HandleError {
        message: String,
        request_seq: Option<RequestSequence>,
    },
}

impl Display for Command {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Command::UpdateSubscriptionState { .. } => f.write_str("UpdateSubscription"),
            Command::UpdateEntriesState { .. } => f.write_str("UpdateEntries"),
            Command::CompleteDevieAuthorizationFlow { .. } => {
                f.write_str("CompleteDeviceAuthorizationFlow")
            }
            cmd => write!(f, "{cmd:?}"),
        }
    }
}

impl Command {
    pub fn quit() -> Self {
        Command::Quit
    }
    pub fn authenticate() -> Self {
        Command::Authenticate
    }
    pub fn move_right_tab_selection() -> Self {
        Command::MoveTabSelection(Direction::Right)
    }
    pub fn move_left_tab_selection() -> Self {
        Command::MoveTabSelection(Direction::Left)
    }
    pub fn move_up_authentication_provider() -> Self {
        Command::MoveAuthenticationProvider(Direction::Up)
    }
    pub fn move_down_authentication_provider() -> Self {
        Command::MoveAuthenticationProvider(Direction::Down)
    }
    pub fn move_up_entry() -> Self {
        Command::MoveEntry(Direction::Up)
    }
    pub fn move_down_entry() -> Self {
        Command::MoveEntry(Direction::Down)
    }
    pub fn reload_entries() -> Self {
        Command::ReloadEntries
    }
    pub fn open_entry() -> Self {
        Command::OpenEntry
    }
    pub fn move_entry_first() -> Self {
        Command::MoveEntryFirst
    }
    pub fn move_entry_last() -> Self {
        Command::MoveEntryLast
    }
    pub fn prompt_feed_subscription() -> Self {
        Command::PromptFeedSubscription
    }
    pub fn prompt_feed_edition() -> Self {
        Command::PromptFeedEdition
    }
    pub fn prompt_feed_unsubscription() -> Self {
        Command::PromptFeedUnsubscription
    }
    pub fn move_up_subscribed_feed() -> Self {
        Command::MoveSubscribedFeed(Direction::Up)
    }
    pub fn move_down_subscribed_feed() -> Self {
        Command::MoveSubscribedFeed(Direction::Down)
    }
    pub fn reload_subscription() -> Self {
        Command::ReloadSubscription
    }
    pub fn open_feed() -> Self {
        Command::OpenFeed
    }
    pub fn move_subscribed_feed_first() -> Self {
        Command::MoveSubscribedFeedFirst
    }
    pub fn move_subscribed_feed_last() -> Self {
        Command::MoveSubscribedFeedLast
    }
    pub fn move_filter_requirement_left() -> Self {
        Command::MoveFilterRequirement(Direction::Left)
    }
    pub fn move_filter_requirement_right() -> Self {
        Command::MoveFilterRequirement(Direction::Right)
    }
    pub fn activate_category_filtering() -> Self {
        Command::ActivateCategoryFilterling
    }
    pub fn deactivate_category_filtering() -> Self {
        Command::DeactivateCategoryFiltering
    }
}