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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::VTTCueBinding::{
    self, AlignSetting, AutoKeyword, DirectionSetting, LineAlignSetting, PositionAlignSetting,
    VTTCueMethods,
};
use crate::dom::bindings::error::{Error, ErrorResult};
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::documentfragment::DocumentFragment;
use crate::dom::globalscope::GlobalScope;
use crate::dom::texttrackcue::TextTrackCue;
use crate::dom::vttregion::VTTRegion;
use crate::dom::window::Window;
use dom_struct::dom_struct;
use std::cell::Cell;

#[dom_struct]
pub struct VTTCue {
    texttrackcue: TextTrackCue,
    region: DomRefCell<Option<Dom<VTTRegion>>>,
    vertical: Cell<DirectionSetting>,
    snap_to_lines: Cell<bool>,
    line: DomRefCell<LineAndPositionSetting>,
    line_align: Cell<LineAlignSetting>,
    position: DomRefCell<LineAndPositionSetting>,
    position_align: Cell<PositionAlignSetting>,
    size: Cell<f64>,
    align: Cell<AlignSetting>,
    text: DomRefCell<DOMString>,
}

impl VTTCue {
    pub fn new_inherited(start_time: f64, end_time: f64, text: DOMString) -> Self {
        VTTCue {
            texttrackcue: TextTrackCue::new_inherited(
                DOMString::default(),
                start_time,
                end_time,
                None,
            ),
            region: DomRefCell::new(None),
            vertical: Cell::new(DirectionSetting::default()),
            snap_to_lines: Cell::new(true),
            line: DomRefCell::new(LineAndPositionSetting::Auto),
            line_align: Cell::new(LineAlignSetting::Start),
            position: DomRefCell::new(LineAndPositionSetting::Auto),
            position_align: Cell::new(PositionAlignSetting::Auto),
            size: Cell::new(100_f64),
            align: Cell::new(AlignSetting::Center),
            text: DomRefCell::new(text),
        }
    }

    pub fn new(
        global: &GlobalScope,
        start_time: f64,
        end_time: f64,
        text: DOMString,
    ) -> DomRoot<Self> {
        reflect_dom_object(
            Box::new(Self::new_inherited(start_time, end_time, text)),
            global,
        )
    }

    #[allow(non_snake_case)]
    pub fn Constructor(
        window: &Window,
        start_time: Finite<f64>,
        end_time: Finite<f64>,
        text: DOMString,
    ) -> DomRoot<Self> {
        VTTCue::new(&window.global(), *start_time, *end_time, text)
    }
}

impl VTTCueMethods for VTTCue {
    // https://w3c.github.io/webvtt/#dom-vttcue-region
    fn GetRegion(&self) -> Option<DomRoot<VTTRegion>> {
        self.region
            .borrow()
            .as_ref()
            .map(|r| DomRoot::from_ref(&**r))
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-region
    fn SetRegion(&self, value: Option<&VTTRegion>) {
        *self.region.borrow_mut() = value.map(|r| Dom::from_ref(r))
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-vertical
    fn Vertical(&self) -> DirectionSetting {
        self.vertical.get()
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-vertical
    fn SetVertical(&self, value: DirectionSetting) {
        self.vertical.set(value);
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-snaptolines
    fn SnapToLines(&self) -> bool {
        self.snap_to_lines.get()
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-snaptolines
    fn SetSnapToLines(&self, value: bool) {
        self.snap_to_lines.set(value)
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-line
    fn Line(&self) -> VTTCueBinding::LineAndPositionSetting {
        VTTCueBinding::LineAndPositionSetting::from(self.line.borrow().clone())
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-line
    fn SetLine(&self, value: VTTCueBinding::LineAndPositionSetting) {
        *self.line.borrow_mut() = value.into();
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-linealign
    fn LineAlign(&self) -> LineAlignSetting {
        self.line_align.get()
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-linealign
    fn SetLineAlign(&self, value: LineAlignSetting) {
        self.line_align.set(value);
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-position
    fn Position(&self) -> VTTCueBinding::LineAndPositionSetting {
        VTTCueBinding::LineAndPositionSetting::from(self.position.borrow().clone())
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-position
    fn SetPosition(&self, value: VTTCueBinding::LineAndPositionSetting) -> ErrorResult {
        if let VTTCueBinding::LineAndPositionSetting::Double(x) = value {
            if *x < 0_f64 || *x > 100_f64 {
                return Err(Error::IndexSize);
            }
        }

        *self.position.borrow_mut() = value.into();
        Ok(())
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-positionalign
    fn PositionAlign(&self) -> PositionAlignSetting {
        self.position_align.get()
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-positionalign
    fn SetPositionAlign(&self, value: PositionAlignSetting) {
        self.position_align.set(value);
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-size
    fn Size(&self) -> Finite<f64> {
        Finite::wrap(self.size.get())
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-size
    fn SetSize(&self, value: Finite<f64>) -> ErrorResult {
        if *value < 0_f64 || *value > 100_f64 {
            return Err(Error::IndexSize);
        }

        self.size.set(*value);
        Ok(())
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-align
    fn Align(&self) -> AlignSetting {
        self.align.get()
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-align
    fn SetAlign(&self, value: AlignSetting) {
        self.align.set(value);
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-text
    fn Text(&self) -> DOMString {
        self.text.borrow().clone()
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-text
    fn SetText(&self, value: DOMString) {
        *self.text.borrow_mut() = value;
    }

    // https://w3c.github.io/webvtt/#dom-vttcue-getcueashtml
    fn GetCueAsHTML(&self) -> DomRoot<DocumentFragment> {
        todo!()
    }
}

#[derive(Clone, JSTraceable, MallocSizeOf)]
enum LineAndPositionSetting {
    Double(f64),
    Auto,
}

impl From<VTTCueBinding::LineAndPositionSetting> for LineAndPositionSetting {
    fn from(value: VTTCueBinding::LineAndPositionSetting) -> Self {
        match value {
            VTTCueBinding::LineAndPositionSetting::Double(x) => LineAndPositionSetting::Double(*x),
            VTTCueBinding::LineAndPositionSetting::AutoKeyword(_) => LineAndPositionSetting::Auto,
        }
    }
}

impl From<LineAndPositionSetting> for VTTCueBinding::LineAndPositionSetting {
    fn from(value: LineAndPositionSetting) -> Self {
        match value {
            LineAndPositionSetting::Double(x) => {
                VTTCueBinding::LineAndPositionSetting::Double(Finite::wrap(x))
            },
            LineAndPositionSetting::Auto => {
                VTTCueBinding::LineAndPositionSetting::AutoKeyword(AutoKeyword::Auto)
            },
        }
    }
}