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 super::{Imgs, Message, FILL_FRAC_ONE};
use crate::ui::{
    fonts::IcedFonts as Fonts,
    ice::{component::neat_button, style, Element},
};
use i18n::Localization;
use iced::{
    button, scrollable, Align, Button, Column, Container, Length, Row, Scrollable, Space, Text,
};

pub struct Screen {
    back_button: button::State,
    delete_button: button::State,
    server_buttons: Vec<button::State>,
    servers_list: scrollable::State,
}

impl Screen {
    pub fn new() -> Self {
        Self {
            back_button: Default::default(),
            delete_button: Default::default(),
            server_buttons: vec![],
            servers_list: Default::default(),
        }
    }

    pub(super) fn view(
        &mut self,
        fonts: &Fonts,
        imgs: &Imgs,
        servers: &[impl AsRef<str>],
        selected_server_index: Option<usize>,
        i18n: &Localization,
        button_style: style::button::Style,
    ) -> Element<Message> {
        let title = Text::new(i18n.get_msg("main-servers-select_server"))
            .size(fonts.cyri.scale(35))
            .width(Length::Fill)
            .horizontal_alignment(iced::HorizontalAlignment::Center);

        let back_button = Container::new(
            Container::new(neat_button(
                &mut self.back_button,
                i18n.get_msg("common-back"),
                FILL_FRAC_ONE,
                button_style,
                Some(Message::Back),
            ))
            .max_width(200),
        )
        .width(Length::Fill)
        .align_x(Align::Center);

        let delete_button = Container::new(
            Container::new(neat_button(
                &mut self.delete_button,
                i18n.get_msg("common-delete_server"),
                FILL_FRAC_ONE,
                button_style,
                Some(Message::DeleteServer),
            ))
            .max_width(200),
        )
        .width(Length::Fill)
        .align_x(Align::Center);

        let mut list = Scrollable::new(&mut self.servers_list)
            .spacing(8)
            .align_items(Align::Start)
            .width(Length::Fill)
            .height(Length::Fill);

        // Reset button states if servers were added / removed
        if self.server_buttons.len() != servers.len() {
            self.server_buttons = vec![Default::default(); servers.len()];
        }

        let list_items =
            self.server_buttons
                .iter_mut()
                .zip(servers)
                .enumerate()
                .map(|(i, (state, server))| {
                    let color = if Some(i) == selected_server_index {
                        (97, 255, 18)
                    } else {
                        (97, 97, 25)
                    };
                    let button = Button::new(
                        state,
                        Row::with_children(vec![
                            Space::new(Length::FillPortion(5), Length::Units(0)).into(),
                            Text::new(server.as_ref())
                                .size(fonts.cyri.scale(30))
                                .width(Length::FillPortion(95))
                                .vertical_alignment(iced::VerticalAlignment::Center)
                                .into(),
                        ]),
                    )
                    .style(
                        style::button::Style::new(imgs.selection)
                            .hover_image(imgs.selection_hover)
                            .press_image(imgs.selection_press)
                            .image_color(vek::Rgba::new(color.0, color.1, color.2, 255)),
                    )
                    .min_height(100)
                    .on_press(Message::ServerChanged(i));
                    Row::with_children(vec![
                        Space::new(Length::FillPortion(3), Length::Units(0)).into(),
                        button.width(Length::FillPortion(92)).into(),
                        Space::new(Length::FillPortion(5), Length::Units(0)).into(),
                    ])
                });

        for item in list_items {
            list = list.push(item);
        }

        Container::new(
            Container::new(
                Column::with_children(vec![
                    title.into(),
                    list.into(),
                    Row::with_children(vec![delete_button.into(), back_button.into()])
                        .width(Length::Fill)
                        .into(),
                ])
                .width(Length::Fill)
                .height(Length::Fill)
                .spacing(10)
                .padding(20),
            )
            .style(
                style::container::Style::color_with_double_cornerless_border(
                    (22, 18, 16, 255).into(),
                    (11, 11, 11, 255).into(),
                    (54, 46, 38, 255).into(),
                ),
            )
            .max_width(500),
        )
        .width(Length::Fill)
        .align_x(Align::Center)
        .padding(80)
        .into()
    }
}