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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
pub use crate::control_flow::{lens, Close, Escape, EscapeOrStart, LensFns, LoopControl};
use crate::{
    align::Alignment,
    border::BorderStyle,
    control_flow::{unboxed, Lens, OrClose, OrEscape, OrEscapeOrStart},
    pad_by::Padding,
};
pub use chargrid_core::app;
use chargrid_core::{
    input, BoxedComponent, Component, Coord, Ctx, Event, FrameBuffer, Rgba32, Size, Style, Tint,
};
use std::time::Duration;

pub struct CF<O, S>(unboxed::CF<BoxedComponent<O, S>>);

pub fn cf<C: 'static + Component>(component: C) -> CF<C::Output, C::State>
where
    C::State: Sized,
{
    CF(unboxed::cf(BoxedComponent(Box::new(component))))
}

impl<O, S> From<BoxedComponent<O, S>> for CF<O, S> {
    fn from(boxed_component: BoxedComponent<O, S>) -> Self {
        CF(unboxed::cf(boxed_component))
    }
}

impl<O, S> Component for CF<O, S> {
    type Output = O;
    type State = S;
    fn render(&self, state: &Self::State, ctx: Ctx, fb: &mut FrameBuffer) {
        self.0.render(state, ctx, fb);
    }
    fn update(&mut self, state: &mut Self::State, ctx: Ctx, event: Event) -> Self::Output {
        self.0.update(state, ctx, event)
    }
    fn size(&self, state: &Self::State, ctx: Ctx) -> Size {
        self.0.size(state, ctx)
    }
}

impl<O: 'static, S: 'static> CF<O, S> {
    pub fn lens_state<S_: 'static, L: 'static>(self, lens: L) -> CF<O, S_>
    where
        L: Lens<Input = S_, Output = S>,
    {
        self.0.lens_state(lens).boxed()
    }

    pub fn some(self) -> CF<Option<O>, S> {
        self.0.some().boxed()
    }

    pub fn none(self) -> CF<Option<O>, S> {
        self.0.none().boxed()
    }

    pub fn overlay_tint<D: 'static + Component<State = S>, T: 'static + Tint>(
        self,
        background: D,
        tint: T,
        depth_delta: i8,
    ) -> Self {
        self.0.overlay_tint(background, tint, depth_delta).boxed()
    }

    pub fn overlay<D: 'static + Component<State = S>>(
        self,
        background: D,
        depth_delta: i8,
    ) -> Self {
        self.0.overlay(background, depth_delta).boxed()
    }

    pub fn clear_each_frame(self) -> Self {
        self.0.clear_each_frame().boxed()
    }

    pub fn fill(self, background: Rgba32) -> Self {
        self.0.fill(background).boxed()
    }

    pub fn border(self, style: BorderStyle) -> Self {
        self.0.border(style).boxed()
    }

    pub fn pad_to(self, size: Size) -> Self {
        self.0.pad_to(size).boxed()
    }

    pub fn pad_by(self, padding: Padding) -> Self {
        self.0.pad_by(padding).boxed()
    }

    pub fn align(self, alignment: Alignment) -> Self {
        self.0.align(alignment).boxed()
    }

    pub fn centre(self) -> Self {
        self.0.centre().boxed()
    }

    pub fn add_offset(self, offset: Coord) -> Self {
        self.0.add_offset(offset).boxed()
    }

    pub fn add_x(self, x: i32) -> Self {
        self.0.add_x(x).boxed()
    }

    pub fn add_y(self, y: i32) -> Self {
        self.0.add_y(y).boxed()
    }

    pub fn set_size(self, size: Size) -> Self {
        self.0.set_size(size).boxed()
    }

    pub fn set_width(self, width: u32) -> Self {
        self.0.set_width(width).boxed()
    }

    pub fn set_height(self, height: u32) -> Self {
        self.0.set_height(height).boxed()
    }

    pub fn bound_size(self, size: Size) -> Self {
        self.0.bound_size(size).boxed()
    }

    pub fn bound_width(self, width: u32) -> Self {
        self.0.bound_width(width).boxed()
    }

    pub fn bound_height(self, height: u32) -> Self {
        self.0.bound_height(height).boxed()
    }

    pub fn with_title<T: 'static + Component<State = S>>(self, title: T, padding: i32) -> Self {
        self.0.with_title(title, padding).boxed()
    }
}

impl<O: 'static, S: 'static> CF<O, S> {
    pub fn with_state(self, state: S) -> CF<O, ()> {
        self.0.with_state(state).boxed()
    }
}

impl<S: 'static> CF<(), S> {
    pub fn delay(self, duration: Duration) -> CF<Option<()>, S> {
        self.0.delay(duration).boxed()
    }

    pub fn press_any_key(self) -> CF<Option<()>, S> {
        self.0.press_any_key().boxed()
    }
}

impl<T: 'static, S: 'static> CF<Option<T>, S> {
    pub fn and_then_persistent<U, D: 'static, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        D: Component<Output = Option<U>, State = S>,
        F: FnOnce(Self, T) -> D,
    {
        self.0.and_then_persistent(|a, b| f(a.into(), b)).boxed()
    }

    pub fn and_then<U, D: 'static, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        D: Component<Output = Option<U>, State = S>,
        F: FnOnce(T) -> D,
    {
        self.0.and_then(f).boxed()
    }

    pub fn and_then_side_effect<U, D: 'static, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        D: Component<Output = Option<U>, State = S>,
        F: FnOnce(T, &mut S) -> D,
    {
        self.0.and_then_side_effect(f).boxed()
    }

    pub fn then<U, D: 'static, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        D: Component<Output = Option<U>, State = S>,
        F: FnOnce() -> D,
    {
        self.0.then(f).boxed()
    }

    pub fn then_side_effect<U, D: 'static, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        D: Component<Output = Option<U>, State = S>,
        F: FnOnce(&mut S) -> D,
    {
        self.0.then_side_effect(f).boxed()
    }

    pub fn side_effect<F: 'static>(self, f: F) -> CF<Option<T>, S>
    where
        F: FnOnce(&mut S),
    {
        self.0.side_effect(f).boxed()
    }

    pub fn map<U, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        F: FnOnce(T) -> U,
    {
        self.0.map(f).boxed()
    }

    pub fn map_val<U, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        F: FnOnce() -> U,
    {
        self.0.map_val(f).boxed()
    }

    pub fn map_side_effect<U, F: 'static>(self, f: F) -> CF<Option<U>, S>
    where
        F: FnOnce(T, &mut S) -> U,
    {
        self.0.map_side_effect(f).boxed()
    }

    pub fn catch_escape(self) -> CF<Option<OrEscape<T>>, S> {
        self.0.catch_escape().boxed()
    }

    pub fn catch_escape_or_start(self) -> CF<Option<OrEscapeOrStart<T>>, S> {
        self.0.catch_escape_or_start().boxed()
    }

    pub fn menu_harness(self) -> CF<Option<OrClose<T>>, S> {
        self.0.menu_harness().boxed()
    }

    pub fn no_peek(self) -> CF<Option<T>, S> {
        self.0.no_peek().boxed()
    }

    pub fn replace<U: 'static>(self, value: U) -> CF<Option<U>, S> {
        self.0.replace(value).boxed()
    }

    pub fn continue_<Br: 'static>(self) -> CF<Option<LoopControl<T, Br>>, S> {
        self.0.continue_().boxed()
    }

    pub fn break_<Co: 'static>(self) -> CF<Option<LoopControl<Co, T>>, S> {
        self.0.break_().boxed()
    }

    pub fn continue_with<Br: 'static, U: 'static>(
        self,
        value: U,
    ) -> CF<Option<LoopControl<U, Br>>, S> {
        self.0.continue_with(value).boxed()
    }

    pub fn break_with<Co: 'static, U: 'static>(
        self,
        value: U,
    ) -> CF<Option<LoopControl<Co, U>>, S> {
        self.0.break_with(value).boxed()
    }

    pub fn repeat<A: 'static, O: 'static, F: 'static>(self, init: A, f: F) -> CF<Option<O>, S>
    where
        F: FnMut(A, T) -> CF<Option<LoopControl<A, O>>, S>,
    {
        loop_((self, f, init), |(self_, mut f, acc)| {
            self_.and_then_persistent(|self_, entry| {
                f(acc, entry).map(|loop_control| loop_control.map_continue(|c| (self_, f, c)))
            })
        })
    }

    pub fn repeat_unit<O: 'static, F: 'static>(self, mut f: F) -> CF<Option<O>, S>
    where
        F: FnMut(T) -> CF<Option<LoopControl<(), O>>, S>,
    {
        self.repeat((), move |(), entry| f(entry))
    }

    pub fn pause(self) -> CF<Option<T>, S> {
        self.0.pause().boxed()
    }
}

impl<O: 'static> CF<O, ()> {
    pub fn ignore_state<S: 'static>(self) -> CF<O, S> {
        self.0.ignore_state().boxed()
    }
}

impl<S: 'static> CF<(), S> {
    pub fn ignore_output<O: 'static>(self) -> CF<Option<O>, S> {
        self.0.ignore_output().boxed()
    }
}

impl<S: 'static> CF<app::Output, S> {
    pub fn exit_on_close(self) -> Self {
        self.0.exit_on_close().boxed()
    }
}

pub type App = CF<app::Output, ()>;

pub fn val<S: 'static, T: 'static + Clone>(t: T) -> CF<Option<T>, S> {
    unboxed::val(t).boxed()
}

pub fn val_once<S: 'static, T: 'static>(t: T) -> CF<Option<T>, S> {
    unboxed::val_once(t).boxed()
}

pub fn break_<S: 'static, T: 'static, Co: 'static>(t: T) -> CF<Option<LoopControl<Co, T>>, S> {
    val_once(t).break_()
}

pub fn continue_<S: 'static, T: 'static, Br: 'static>(t: T) -> CF<Option<LoopControl<T, Br>>, S> {
    val_once(t).continue_()
}

pub fn never<S: 'static, T: 'static>() -> CF<Option<T>, S> {
    unboxed::never().boxed()
}

pub fn loop_state<S: 'static, Co, Br, C: 'static, F: 'static>(
    state: S,
    init: Co,
    f: F,
) -> CF<Option<Br>, ()>
where
    C::State: Sized,
    C: Component<Output = Option<LoopControl<Co, Br>>, State = S>,
    F: FnMut(Co) -> C,
{
    unboxed::loop_state(state, init, f).boxed()
}

pub fn loop_<Co, Br, C: 'static, F: 'static>(init: Co, f: F) -> CF<Option<Br>, C::State>
where
    C::State: Sized,
    C: Component<Output = Option<LoopControl<Co, Br>>>,
    F: FnMut(Co) -> C,
{
    unboxed::loop_(init, f).boxed()
}

pub fn loop_unit<Br, C: 'static, F: 'static>(f: F) -> CF<Option<Br>, C::State>
where
    C::State: Sized,
    C: Component<Output = Option<LoopControl<(), Br>>>,
    F: FnMut() -> C,
{
    unboxed::loop_unit(f).boxed()
}

pub fn loop_mut<Co, Br, T: 'static, C: 'static, F: 'static>(
    init: Co,
    value: T,
    f: F,
) -> CF<Option<Br>, C::State>
where
    C::State: Sized,
    C: Component<Output = Option<LoopControl<Co, Br>>>,
    F: FnMut(Co, &mut T) -> C,
{
    unboxed::loop_mut(init, value, f).boxed()
}

pub fn on_state<S: 'static, T: 'static, F: 'static>(f: F) -> CF<Option<T>, S>
where
    F: FnOnce(&mut S) -> T,
{
    unboxed::on_state(f).boxed()
}

pub fn on_state_then<C: 'static, F: 'static>(f: F) -> CF<C::Output, C::State>
where
    C: Component,
    C::State: Sized,
    F: FnOnce(&mut C::State) -> C,
{
    unboxed::on_state_then(f).boxed()
}

pub fn render<F: 'static + Fn(Ctx, &mut FrameBuffer)>(f: F) -> CF<(), ()> {
    unboxed::render(f).boxed()
}

pub fn render_state<S: 'static, F: 'static + Fn(&S, Ctx, &mut FrameBuffer)>(f: F) -> CF<(), S> {
    unboxed::render_state(f).boxed()
}

pub fn unit<S: 'static>() -> CF<(), S> {
    unboxed::unit().boxed()
}

pub fn many<I: 'static, S: 'static, C: 'static>(iterable: I) -> CF<(), S>
where
    C: Component<State = S>,
    for<'a> &'a I: IntoIterator<Item = &'a C>,
    for<'a> &'a mut I: IntoIterator<Item = &'a mut C>,
{
    unboxed::many(iterable).boxed()
}

pub fn styled_string<S: 'static>(string: String, style: Style) -> CF<(), S> {
    unboxed::styled_string(string, style).boxed()
}

pub fn on_input<F: 'static, T, S: 'static>(f: F) -> CF<Option<T>, S>
where
    F: FnMut(input::Input) -> Option<T>,
{
    unboxed::on_input(f).boxed()
}

pub fn on_input_state<F: 'static, T, S: 'static>(f: F) -> CF<Option<T>, S>
where
    F: FnMut(input::Input, &mut S) -> Option<T>,
{
    unboxed::on_input_state(f).boxed()
}