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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#![doc(html_logo_url = "https://raw.githubusercontent.com/ramp-stack/pelican_ui_std/main/logo.png")]

//! Pelican UI
//!
//! Pelican UI is a fast, cross-platform UI renderer and component system for building beautiful and consistent apps.
//!
//! Key features include:
//! - **Theme system**: Easily customize colors, fonts, and more through a central [`Theme`] object.
//! - **Simple components & layouts**: Build UIs quickly with a minimal and intuitive API.
//! - **High performance**: Optimized for speed, making it suitable for both lightweight and complex applications.
//! - **Cross-platform support**: Works seamlessly on desktop, web, and mobile platforms.
//! - **Standard components**: Access ready-to-use components via the [`pelican_ui_std`](<https://docs.rs/crate/pelican_ui_std/latest>) crate built on top of Pelican UI.
//!
//! Check out the [website](http://ramp-stack.com/pelican_ui) for more information, the [Quick Start Guide](http://ramp-stack.com/pelican_ui/getting_started) to set up your first app, and join the [community](https://discord.gg/cTRaRbUZ) if you have questions or want to share ideas.

use std::collections::BTreeMap;
use std::any::TypeId;
use std::sync::Arc;

use wgpu_canvas::{Atlas, Item as CanvasItem, Area};

use maverick_os::window::{Window, Event as WindowEvent, Lifetime};
pub use maverick_os::hardware::Context as HardwareContext;
use maverick_os::runtime::{Services, ServiceList};

pub use maverick_os::hardware;
pub use maverick_os::runtime;
pub use maverick_os::air;
pub use maverick_os::{MaverickOS, start as maverick_start, State};

pub use include_dir::include_dir as include_assets;

pub use pelican_ui_proc::Component;

use downcast_rs::{Downcast, impl_downcast};
use include_dir::{Dir, DirEntry};

mod wgpu;
use wgpu::Canvas;

pub mod events;
use events::{EventHandler, Events, Event, TickEvent};

pub mod layout;
use layout::{Scale, Scaling};

pub mod drawable;
use drawable::{Drawable, _Drawable, SizedBranch};

pub mod resources {
    pub use wgpu_canvas::{Image, Font};
}

mod theme;
pub use theme::{
    Theme,
    Illustrations,
    ColorResources,
    FontResources,
    IconResources,
    LayoutResources,
    ButtonColorScheme,
    BrandColor,
    TextColor,
    BackgroundColor,
    ButtonColors,
    OutlineColor,
    IllustrationColors,
    StatusColor,
};

type PluginList = BTreeMap<TypeId, Box<dyn Plugin>>;

pub trait Plugin: Downcast {
    fn new(ctx: &mut Context) -> Self where Self: Sized;

    fn event(&mut self, _ctx: &mut Context, _event: &dyn Event) {}
}
impl_downcast!(Plugin); 

/// `Assets` stores all the assets required by your project, 
/// including images, fonts, and other resources.
pub struct Assets {
    dirs: Vec<Dir<'static>>,
    atlas: Atlas,
}

impl Default for Assets {
    fn default() -> Self {
        Self::new()
    }
}

impl Assets {
    pub fn new() -> Self {
        Assets {
            dirs: Vec::new(),
            atlas: Atlas::default(),            
        } 
    }

    /// Returns a reference to a vector containing all included directories.
    pub fn dirs(&self) -> &Vec<Dir<'static>> {&self.dirs}
    /// Adds a font to the atlas from the provided byte slice and returns the loaded [`resources::Font`] resource.
    pub fn add_font(&mut self, font: &[u8]) -> resources::Font {self.atlas.add_font(font).unwrap()}
    /// Adds an image to the atlas from the provided [`image::RgbaImage`] and returns the loaded [`resources::Image`] resource.
    pub fn add_image(&mut self, image: image::RgbaImage) -> resources::Image {self.atlas.add_image(image)}
    /// Adds a svg image to the atlas from the provided byte slice and scale factor and returns the loaded [`resources::Image`] resource.
    pub fn add_svg(&mut self, svg: &[u8], scale: f32) -> resources::Image {
        let svg = std::str::from_utf8(svg).unwrap();
        let svg = nsvg::parse_str(svg, nsvg::Units::Pixel, 96.0).unwrap();
        let rgba = svg.rasterize(scale).unwrap();
        let size = rgba.dimensions();
        self.atlas.add_image(image::RgbaImage::from_raw(size.0, size.1, rgba.into_raw()).unwrap())
    }

    /// Loads a font from the given file path and returns an [`Option`] containing the [`resources::Font`] if successful.
    pub fn load_font(&mut self, file: &str) -> Option<resources::Font> {
        self.load_file(file).map(|b| self.add_font(&b))
    }

    /// Loads an image from the given file path and returns an [`Option`] containing the [`resources::Image`] if successful.
    pub fn load_image(&mut self, file: &str) -> Option<resources::Image> {
        self.load_file(file).map(|b|
            self.add_image(image::load_from_memory(&b).unwrap().into())
        )
    }

    /// Loads the contents of the specified file from the search directories, returning its bytes if found.
    pub fn load_file(&self, file: &str) -> Option<Vec<u8>> {
        self.dirs.iter().find_map(|dir|
            dir.find(file).ok().and_then(|mut f|
                f.next().and_then(|f|
                    if let DirEntry::File(f) = f {
                        Some(f.contents().to_vec())
                    } else {
                        None
                    }
                )
            )
        )
    }

    /// Adds a directory to the list of asset search paths.
    pub fn include_assets(&mut self, dir: Dir<'static>) {
        self.dirs.push(dir);
    }
}

pub struct PluginGuard<'a, P: Plugin>(Option<P>, &'a mut Context);
impl<'a, P: Plugin> PluginGuard<'a, P> {
    pub fn get(&mut self) -> (&mut P, &mut Context) {
        (self.0.as_mut().unwrap(), &mut *self.1)
    }
    pub fn run<T>(&mut self, clo: impl FnOnce(&mut P, &mut Context) -> T) -> T {
        clo(self.0.as_mut().unwrap(), self.1)
    }
}
impl<'a, P: Plugin> Drop for PluginGuard<'a, P> {
    fn drop(&mut self) {
        self.1.plugins.insert(TypeId::of::<P>(), Box::new(self.0.take().unwrap()));
    }
}

/// `Context` holds the app context, including hardware, runtime, assets, theme, plugins, events, and state.
pub struct Context {
    pub hardware: HardwareContext,
    pub runtime: runtime::Context,
    pub assets: Assets,
    pub theme: Theme,
    plugins: PluginList,
    events: Events,
    state: Option<State>
}

impl Context {
    /// Creates a new `Context` instance and loads the default Pelican UI assets.
    pub fn new(hardware: HardwareContext, runtime: runtime::Context, state: Option<State>) -> Self {
        let mut assets = Assets::new();
        assets.include_assets(include_assets!("./resources"));
        Context {
            hardware,
            runtime,
            theme: Theme::default(&mut assets),
            assets,  
            plugins: PluginList::new(),
            events: Events::new(),    
            state
        }
    }

    /// Adds an [`Event`] to the context's event queue to be triggered.
    pub fn trigger_event(&mut self, event: impl Event) {
        self.events.push_back(Box::new(event));
    }

    pub fn get<P: Plugin + 'static>(&mut self) -> PluginGuard<'_, P> {
        PluginGuard(Some(*self.plugins.remove(&TypeId::of::<P>())
            .unwrap_or_else(|| panic!("Plugin Not Configured: {:?}", std::any::type_name::<P>()))
            .downcast().ok().unwrap()), self)
    }

    /// Returns a mutable reference to the [`State`]
    pub fn state(&mut self) -> &mut State {
        self.state.as_mut().unwrap()
    }

  //pub fn state(&mut self) -> &mut State {
  //    self.base_context.state()
  //}


  //pub fn add_font(&mut self, font: &[u8]) -> canvas::Font {
  //    self.base_context.as_mut().add_font(font)
  //}

  //pub fn add_image(&mut self, image: image::RgbaImage) -> canvas::Image {
  //    self.base_context.as_mut().add_image(image)
  //}

  //pub fn add_svg(&mut self, svg: &[u8], quality: f32) -> canvas::Image {
  //    self.base_context.as_mut().add_svg(svg, quality)
  //}



    // pub fn as_canvas(&mut self) -> &mut FontAtlas {
    //     self.as_mut()
    // }
}

/// A trait for registering plugins.
///
/// Implementors should return a collection of plugins to be stored in the [`Context`].
///
/// # Example
/// ```
/// struct MyPlugin;
/// impl Plugin for MyPlugin { /* ... */ }
///
/// struct MyApp;
/// impl Plugins for MyApp {
///     fn plugins(ctx: &mut Context) -> Vec<Box<dyn Plugin>> {
///         vec![Box::new(MyPlugin)]
///     }
/// }
/// ```
pub trait Plugins {
    /// Returns a list of plugins for the application.
    fn plugins(ctx: &mut Context) -> Vec<Box<dyn Plugin>>;
}

/// Allow [`Context`] to provide mutable access to the [`Atlas`].
impl AsMut<Atlas> for Context {fn as_mut(&mut self) -> &mut Atlas {&mut self.assets.atlas}}

/// The core application trait.
///
/// An `Application` provides services, registers plugins, and defines
/// the entrypoint for creating the root [`Drawable`] of the app.
///
/// # Example
/// ```
/// struct MyApp;
/// impl Services for MyApp {
///     fn services() -> ServiceList { ServiceList::new() }
/// }
///
/// impl Plugins for MyApp {
///     fn plugins(ctx: &mut Context) -> Vec<Box<dyn Plugin>> { vec![] }
/// }
///
/// impl Application for MyApp {
///     async fn new(ctx: &mut Context) -> Box<dyn Drawable> {
///         Box::new(MyRootDrawable::new())
///     }
/// }
/// ```
pub trait Application: Services + Plugins {
    fn new(ctx: &mut Context) -> impl Future<Output = Box<dyn Drawable>>;
}

/// Provide [`Services`] for [`PelicanEngine`] by deferring to the application type.
impl<A: Application> Services for PelicanEngine<A> {
    fn services() -> ServiceList { A::services() }
}


/// The main engine type.
///
/// `PelicanEngine` wires together the [`Application`], windowing system,
/// plugin management, drawing, and event handling.
pub struct PelicanEngine<A: Application> {
    _p: std::marker::PhantomData<A>,
    scale: Scale,
    canvas: Canvas,
    screen: (f32, f32),
    context: Context,
    sized_app: SizedBranch,
    application: Box<dyn Drawable>,
    event_handler: EventHandler,
    items: Vec<(Area, CanvasItem)>,
}

impl<A: Application> maverick_os::Application for PelicanEngine<A> {
    /// Initializes the engine with the given MaverickOS context.
    ///
    /// - Creates a canvas bound to the OS window.
    /// - Constructs an application via [`Application::new`].
    /// - Registers plugins returned by [`Application::plugins`].
    async fn new(ctx: &mut maverick_os::Context) -> Self {
        ctx.hardware.register_notifs();
        let size = ctx.window.size;
        let (canvas, size) = Canvas::new(ctx.window.handle.clone(), size.0, size.1).await;
        let scale = Scale(ctx.window.scale_factor);
        let screen = (scale.logical(size.0 as f32), scale.logical(size.1 as f32));
        let mut context = Context::new(ctx.hardware.clone(), ctx.runtime.clone(), ctx.state.take());
        let plugins = A::plugins(&mut context);
        context.plugins = plugins.into_iter().map(|p| ((*p).type_id(), p)).collect();
        let mut application = A::new(&mut context).await;
        let size_request = _Drawable::request_size(&*application, &mut context);
        let sized_app = application.build(&mut context, screen, size_request);
        ctx.state = context.state.take();
        PelicanEngine{
            _p: std::marker::PhantomData::<A>,
            scale,
            canvas,
            screen,
            context,
            sized_app,
            application,
            event_handler: EventHandler::new(),
            items: Vec::new()
        }
    }
        
    async fn on_event(&mut self, context: &mut maverick_os::Context, event: WindowEvent) {
        self.context.state = context.state.take();
        match event {
            WindowEvent::Lifetime(lifetime) => match lifetime {
                Lifetime::Resized => {
                    self.scale.0 = context.window.scale_factor;
                    let size = context.window.size;
                    let size = self.canvas.resize::<Arc<Window>>(None, size.0, size.1);
                    let size = (self.scale.logical(size.0 as f32), self.scale.logical(size.1 as f32));
                    self.screen = size;
                },
                Lifetime::Resumed => {
                    let _ = self.items.drain(..);
                    self.scale.0 = context.window.scale_factor;
                    let size = context.window.size;
                    let size = self.canvas.resize(Some(context.window.handle.clone()), size.0, size.1);
                    let size = (self.scale.logical(size.0 as f32), self.scale.logical(size.1 as f32));
                    self.screen = size;
                },
                Lifetime::Paused => {},
                Lifetime::Close => {},
                Lifetime::Draw => {//Size before events because the events are given between
                                   //resizing

                    let result = self.event_handler.on_input(&self.scale, maverick_os::window::Input::Tick);
                    if let Some(event) = result {
                        self.context.events.push_back(event);
                    }
                    self.application.event(&mut self.context, self.sized_app.clone(), Box::new(TickEvent));

                    while let Some(event) = self.context.events.pop_front() {
                        if let Some(event) = event
                            .pass(&mut self.context, vec![((0.0, 0.0), self.sized_app.0)])
                            .remove(0)
                        {
                            for id in self.context.plugins.keys().copied().collect::<Vec<_>>() {
                                let mut plugin = self.context.plugins.remove(&id).unwrap();
                                plugin.event(&mut self.context, &*event);    
                                self.context.plugins.insert(id, plugin);
                            }
                            self.application.event(&mut self.context, self.sized_app.clone(), event);
                        }
                    }

                    let size_request = _Drawable::request_size(&*self.application, &mut self.context);
                    self.sized_app = self.application.build(&mut self.context, self.screen, size_request);
                    let drawn = self.application.draw(self.sized_app.clone(), (0.0, 0.0), (0.0, 0.0, self.screen.0, self.screen.1));
                    let items: Vec<_> = drawn.into_iter().map(|(a, i)| (a.scale(&self.scale), i.scale(&self.scale))).collect();
                    if self.items != items {
                        self.items = items.clone();
                        self.canvas.draw(&mut self.context.assets.atlas, items);
                    }
                },
                Lifetime::MemoryWarning => {},
            },
            WindowEvent::Input(input) => {if let Some(event) = self.event_handler.on_input(&self.scale, input) {self.context.events.push_back(event)}}
        }
        context.state = self.context.state.take();
    }
}




#[macro_export]
macro_rules! start {
    ($app:ty) => {
        maverick_start!(PelicanEngine<$app>);
    };
}