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
use crate::{
    css::css_manager::CssManager,
    fetch::request_builder::{RequestBody, RequestBuilder},
    Context, Css, Dependencies, DropResource, FutureBox, Instant, JsJson, WebsocketMessage,
};
use std::cell::RefCell;
use std::{future::Future, pin::Pin, rc::Rc};

use crate::driver_module::dom::DriverDom;
use crate::{driver_module::api::ApiImport, driver_module::utils::futures_spawn::spawn_local};

use super::api::DomAccess;

#[derive(Debug, Clone, Copy)]
pub enum FetchMethod {
    GET,
    POST,
}

impl FetchMethod {
    pub fn to_str(&self) -> String {
        match self {
            Self::GET => "GET",
            Self::POST => "POST",
        }
        .into()
    }
}

type Executable = dyn Fn(Pin<Box<dyn Future<Output = ()> + 'static>>);
type PlainHandler = dyn Fn(&str) -> Option<String>;

pub struct DriverInner {
    pub(crate) api: ApiImport,
    pub(crate) dependencies: &'static Dependencies,
    pub(crate) css_manager: CssManager,
    pub(crate) dom: &'static DriverDom,
    spawn_executor: Rc<Executable>,
    _subscribe: DropResource,
    _plains_handler: RefCell<Option<Rc<PlainHandler>>>,
}

impl DriverInner {
    pub fn new() -> &'static Self {
        let dependencies: &'static Dependencies = Box::leak(Box::default());

        let api = ApiImport::default();

        let spawn_executor = {
            let api = api.clone();

            Rc::new(move |fut: Pin<Box<dyn Future<Output = ()> + 'static>>| {
                spawn_local(api.clone(), fut);
            })
        };

        let dom = DriverDom::new(&api);
        let css_manager = {
            let driver_dom = dom;
            CssManager::new(move |selector: &str, value: &str| {
                driver_dom.insert_css(selector, value);
            })
        };

        let subscribe = dependencies.hooks.on_after_transaction(move || {
            dom.flush_dom_changes();
        });

        Box::leak(Box::new(DriverInner {
            api,
            dependencies,
            css_manager,
            dom,
            spawn_executor,
            _subscribe: subscribe,
            _plains_handler: RefCell::new(None),
        }))
    }
}

/// Result from request made using [RequestBuilder].
///
/// Variants:
/// - `Ok(status_code, response)` if request succeeded,
/// - `Err(response)` if request failed (because of network error for example).
pub type FetchResult = Result<(u32, RequestBody), String>;

/// Main connection to vertigo facilities - dependencies and rendering client (the browser).
#[derive(Clone, Copy)]
pub struct Driver {
    pub(crate) inner: &'static DriverInner,
}

impl Default for Driver {
    fn default() -> Self {
        let driver = DriverInner::new();

        Driver { inner: driver }
    }
}

impl Driver {
    /// Gets a cookie by name
    pub fn cookie_get(&self, cname: &str) -> String {
        self.inner.api.cookie_get(cname)
    }

    /// Gets a JsJson cookie by name
    pub fn cookie_get_json(&self, cname: &str) -> JsJson {
        self.inner.api.cookie_get_json(cname)
    }

    /// Sets a cookie under provided name
    pub fn cookie_set(&self, cname: &str, cvalue: &str, expires_in: u64) {
        self.inner.api.cookie_set(cname, cvalue, expires_in)
    }

    /// Sets a cookie under provided name
    pub fn cookie_set_json(&self, cname: &str, cvalue: JsJson, expires_in: u64) {
        self.inner.api.cookie_set_json(cname, cvalue, expires_in)
    }

    /// Go back in client's (browser's) history
    pub fn history_back(&self) {
        self.inner.api.history_back();
    }

    /// Make `func` fire every `time` seconds.
    #[must_use]
    pub fn set_interval(&self, time: u32, func: impl Fn() + 'static) -> DropResource {
        self.inner.api.interval_set(time, func)
    }

    /// Gets current value of monotonic clock.
    pub fn now(&self) -> Instant {
        Instant::now(self.inner.api.clone())
    }

    /// Create new RequestBuilder for GETs (more complex version of [fetch](struct.Driver.html#method.fetch))
    #[must_use]
    pub fn request_get(&self, url: impl Into<String>) -> RequestBuilder {
        RequestBuilder::get(url)
    }

    /// Create new RequestBuilder for POSTs (more complex version of [fetch](struct.Driver.html#method.fetch))
    #[must_use]
    pub fn request_post(&self, url: impl Into<String>) -> RequestBuilder {
        RequestBuilder::post(url)
    }

    #[must_use]
    pub fn sleep(&self, time: u32) -> FutureBox<()> {
        let (sender, future) = FutureBox::new();
        self.inner.api.set_timeout_and_detach(time, move || {
            sender.publish(());
        });

        future
    }

    pub fn get_random(&self, min: u32, max: u32) -> u32 {
        self.inner.api.get_random(min, max)
    }

    pub fn get_random_from<K: Clone>(&self, list: &[K]) -> Option<K> {
        let len = list.len();

        if len < 1 {
            return None;
        }

        let max_index = len - 1;

        let index = self.get_random(0, max_index as u32);
        Some(list[index as usize].clone())
    }

    /// Initiate a websocket connection. Provided callback should handle a single [WebsocketMessage].
    #[must_use]
    pub fn websocket<F: Fn(WebsocketMessage) + 'static>(
        &self,
        host: impl Into<String>,
        callback: F,
    ) -> DropResource {
        self.inner.api.websocket(host, callback)
    }

    /// Spawn a future - thus allowing to fire async functions in, for example, event handler. Handy when fetching resources from internet.
    pub fn spawn(&self, future: impl Future<Output = ()> + 'static) {
        let future = Box::pin(future);
        let spawn_executor = self.inner.spawn_executor.clone();
        spawn_executor(future);
    }

    /// Fire provided function in a way that all changes in [dependency graph](struct.Dependencies.html) made by this function
    /// will trigger only one run of updates, just like the changes were done all at once.
    pub fn transaction<R, F: FnOnce(&Context) -> R>(&self, func: F) -> R {
        self.inner.dependencies.transaction(func)
    }

    pub fn dom_access(&self) -> DomAccess {
        self.inner.api.dom_access()
    }

    /// Function added for diagnostic purposes. It allows you to check whether a block with a transaction is missing somewhere.
    pub fn on_after_transaction(&self, callback: impl Fn() + 'static) -> DropResource {
        self.inner.dependencies.hooks.on_after_transaction(callback)
    }

    /// Return true if the code is executed client-side (in the browser).
    ///
    /// ```rust
    /// use vertigo::{dom, get_driver};
    ///
    /// let component = if get_driver().is_browser() {
    ///     dom! { <div>"My dynamic component"</div> }
    /// } else {
    ///     dom! { <div>"Loading... (if not loaded check if JavaScript is enabled)"</div> }
    /// };
    /// ```
    pub fn is_browser(&self) -> bool {
        self.inner.api.is_browser()
    }

    pub fn is_server(&self) -> bool {
        !self.is_browser()
    }

    pub fn env(&self, name: impl Into<String>) -> Option<String> {
        let name = name.into();
        self.inner.api.get_env(name)
    }

    /// Register handler that intercepts defined urls and generates plaintext responses during SSR.
    ///
    /// Return None in the handler if regular HTML should be generated by the App.
    ///
    /// ```rust
    /// use vertigo::get_driver;
    ///
    /// get_driver().plains(|url| {
    ///    if url == "/robots.txt" {
    ///       Some("User-Agent: *\nDisallow: /search".to_string())
    ///    } else {
    ///       None
    ///    }
    /// });
    /// ```
    pub fn plains(&mut self, callback: impl Fn(&str) -> Option<String> + 'static) {
        let mut mur_plains = self.inner._plains_handler.borrow_mut();
        *mur_plains = Some(Rc::new(callback));
    }

    pub fn try_get_plain(&self) {
        if self.is_server() {
            let url = self.inner.api.get_history_location();
            match self.inner._plains_handler.try_borrow() {
                Ok(callback_ref) => {
                    if let Some(callback) = callback_ref.as_deref() {
                        if let Some(body) = callback(&url) {
                            self.inner.api.plain_response(body)
                        }
                    }
                }
                Err(err) => log::error!("Error invoking plains: {err}"),
            }
        } else {
            log::info!("Browser mode, not invoking try_get_plain");
        }
    }

    /// Adds this CSS to manager producing a class name, which is returned
    /// 
    /// There shouldn't be need to use it manually. It's used by `css!` macro.
    pub fn class_name_for(&mut self, css: &Css) -> String {
        self.inner.css_manager.get_class_name(css)
    }
}