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
use crate::{
    widgets::{NcSelector, NcSelectorItem, NcSelectorOptions},
    NcChannels, NcPlane, NcResult, NcString,
};

/// A handy builder for [`NcSelector`].
///
#[derive(Default, Debug)]
pub struct NcSelectorBuilder {
    title: Option<NcString>,
    secondary: Option<NcString>,
    footer: Option<NcString>,
    items: Vec<(NcString, NcString)>,
    default_item: u32,
    max_display: u32,
    channels: [NcChannels; 5],
    flags: u64,
}

impl NcSelectorBuilder {
    /// New `NcSelectorBuilder`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds an item.
    pub fn item(mut self, o: &str, d: &str) -> Self {
        self.items.push((NcString::new(o), NcString::new(d)));
        self
    }

    /// Selects the default item
    ///
    /// It is selected at the start and must be between 0 and itemcount-1.
    //
    // TODO: check when finish it's n-1 at max
    pub fn default_item(mut self, item: u32) -> Self {
        self.default_item = item;
        self
    }

    /// Selects the maximum number of items to display at once.
    ///
    /// 0 uses all available space.
    pub fn max_display(mut self, max: u32) -> Self {
        self.max_display = max;
        self
    }

    /// Sets the title string.
    pub fn title(mut self, title: &str) -> Self {
        self.title = Some(NcString::new(title));
        self
    }

    /// Sets the secondary title string.
    pub fn secondary(mut self, secondary: &str) -> Self {
        self.secondary = Some(NcString::new(secondary));
        self
    }

    /// Sets the footer string.
    pub fn footer(mut self, footer: &str) -> Self {
        self.footer = Some(NcString::new(footer));
        self
    }

    /// Sets the flags.
    pub fn flags(mut self, flags: u64) -> Self {
        self.flags = flags;
        self
    }

    /// Sets all the `NcChannels`.
    pub fn all_channels(
        mut self,
        item_opt: impl Into<NcChannels>,
        item_desc: impl Into<NcChannels>,
        seltitle: impl Into<NcChannels>,
        selfooter: impl Into<NcChannels>,
        selbox: impl Into<NcChannels>,
    ) -> Self {
        self.channels = [
            item_opt.into(),
            item_desc.into(),
            seltitle.into(),
            selfooter.into(),
            selbox.into(),
        ];
        self
    }

    /// Sets the `NcChannels` for the item.
    pub fn item_channels(
        mut self,
        opt: impl Into<NcChannels>,
        desc: impl Into<NcChannels>,
    ) -> Self {
        self.channels[0] = opt.into();
        self.channels[1] = desc.into();
        self
    }

    /// Sets the `NcChannels` for the title.
    pub fn title_channels(mut self, title: impl Into<NcChannels>) -> Self {
        self.channels[2] = title.into();
        self
    }

    /// Sets the `NcChannels` for the secondary title and the footer.
    pub fn secondary_channels(mut self, secondary: impl Into<NcChannels>) -> Self {
        self.channels[3] = secondary.into();
        self
    }

    /// Sets the `NcChannels` for the box title.
    pub fn box_channels(mut self, r#box: impl Into<NcChannels>) -> Self {
        self.channels[4] = r#box.into();
        self
    }

    /// Finishes the builder and returns the `NcSelector`.
    pub fn finish(self, plane: &mut NcPlane) -> NcResult<&mut NcSelector> {
        let mut selitems = vec![];
        for (o, d) in self.items.iter() {
            selitems.push(NcSelectorItem::new(o, d));
        }
        selitems.push(NcSelectorItem::new_empty());

        let default_item = std::cmp::min(self.default_item, selitems.len() as u32 - 1);

        let options = NcSelectorOptions::with_all_options(
            self.title.as_ref(),
            self.secondary.as_ref(),
            self.footer.as_ref(),
            &selitems,
            default_item,
            self.max_display,
            self.channels[0],
            self.channels[1],
            self.channels[2],
            self.channels[3],
            self.channels[4],
        );

        NcSelector::new(plane, &options)
    }
}