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
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned};
use syn::spanned::Spanned;
use syn::Ident;

use crate::widgets::{
    AssignProperty, AssignPropertyAttr, ConditionalBranches, ConditionalWidget, MatchArm,
    Properties, Property, PropertyName, PropertyType, ReturnedWidget, Widget,
};

use super::{assign::AssignInfo, util::WidgetFieldsScope};

impl Property {
    fn update_view_stream(
        &self,
        stream: &mut TokenStream2,
        w_name: &Ident,
        model_name: &Ident,
        conditional_branch: bool,
    ) {
        match &self.ty {
            PropertyType::Assign(assign) => assign.update_view_stream(
                stream,
                &self.name,
                w_name,
                model_name,
                conditional_branch,
            ),
            PropertyType::Widget(widget) => {
                widget.update_view_stream(stream, model_name, conditional_branch);
            }
            PropertyType::ConditionalWidget(cond_widget) => {
                cond_widget.update_view_stream(stream, model_name);
            }
            PropertyType::SignalHandler(_) | PropertyType::ParseError(_) => (),
        }
    }
}

impl Properties {
    fn update_view_stream(
        &self,
        stream: &mut TokenStream2,
        w_name: &Ident,
        model_name: &Ident,
        conditional_branch: bool,
    ) {
        for prop in &self.properties {
            prop.update_view_stream(stream, w_name, model_name, conditional_branch);
        }
    }
}

impl Widget {
    pub(crate) fn init_update_view_stream(&self, stream: &mut TokenStream2, model_name: &Ident) {
        self.update_view_stream(stream, model_name, false);
    }

    fn update_view_stream(
        &self,
        stream: &mut TokenStream2,
        model_name: &Ident,
        conditional_branch: bool,
    ) {
        self.get_template_child_in_scope(stream, WidgetFieldsScope::ViewUpdate);

        let w_name = &self.name;
        self.properties
            .update_view_stream(stream, w_name, model_name, conditional_branch);
        if let Some(returned_widget) = &self.returned_widget {
            returned_widget.update_view_stream(stream, model_name, conditional_branch);
        }
    }
}

impl ConditionalWidget {
    fn update_view_stream(&self, stream: &mut TokenStream2, model_name: &Ident) {
        let brach_stream = match &self.branches {
            ConditionalBranches::If(if_branches) => {
                let mut stream = TokenStream2::new();

                for (index, branch) in if_branches.iter().enumerate() {
                    let mut inner_update_stream = TokenStream2::new();
                    branch
                        .widget
                        .update_view_stream(&mut inner_update_stream, model_name, true);
                    branch.update_stream(&mut stream, &inner_update_stream, index);
                }
                stream
            }
            ConditionalBranches::Match((match_token, expr, match_arms)) => {
                let mut inner_tokens = TokenStream2::new();
                for (index, match_arm) in match_arms.iter().enumerate() {
                    let mut inner_update_stream = TokenStream2::new();
                    match_arm
                        .widget
                        .update_view_stream(&mut inner_update_stream, model_name, true);
                    let MatchArm {
                        pattern,
                        guard,
                        arrow,
                        ..
                    } = match_arm;
                    let (guard_if, guard_expr) = if let Some((guard_if, guard_expr)) = guard {
                        (Some(guard_if), Some(guard_expr))
                    } else {
                        (None, None)
                    };

                    let index = index.to_string();
                    inner_tokens.extend(quote! {
                        #pattern #guard_if #guard_expr #arrow {
                            let __page_active: bool = (__current_page == #index);
                            #inner_update_stream
                            #index
                        },
                    });
                }
                quote! {
                    #match_token #expr {
                        #inner_tokens
                    }
                }
            }
        };

        let w_name = &self.name;
        stream.extend(quote_spanned! {
            w_name.span() =>
            let __current_page = #w_name.visible_child_name().map_or("".to_string(), |s| s.as_str().to_string());
            #w_name.set_visible_child_name(#brach_stream);
        });
    }
}

impl ReturnedWidget {
    fn update_view_stream(
        &self,
        stream: &mut TokenStream2,
        model_name: &Ident,
        conditional_branch: bool,
    ) {
        let w_name = &self.name;
        self.properties
            .update_view_stream(stream, w_name, model_name, conditional_branch);
    }
}

impl AssignProperty {
    fn update_view_stream(
        &self,
        stream: &mut TokenStream2,
        p_name: &PropertyName,
        w_name: &Ident,
        model_name: &Ident,
        conditional_branch: bool,
    ) {
        match &self.attr {
            AssignPropertyAttr::None => (),
            AssignPropertyAttr::Watch => {
                let mut info = AssignInfo {
                    stream,
                    widget_name: w_name,
                    is_conditional: false,
                };
                self.assign_stream(&mut info, p_name, false);
            }
            AssignPropertyAttr::Track((track_stream, paste_model)) => {
                let mut assign_stream = TokenStream2::new();
                let mut info = AssignInfo {
                    stream: &mut assign_stream,
                    widget_name: w_name,
                    is_conditional: false,
                };
                self.assign_stream(&mut info, p_name, false);
                let model = paste_model.then(|| model_name);
                let page_switch = conditional_branch.then(|| {
                    quote_spanned! {
                        p_name.span() =>
                            !__page_active ||
                    }
                });

                stream.extend(quote! {
                    if #page_switch (#model #track_stream) {
                        #assign_stream
                    }
                });
            }
        }
    }
}