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

// src/cli/fmt/fmt_cell.rs

#[derive(Clone)]
pub struct FmtPos {
  pub x: i32,
  pub y: i32
}

#[derive(Clone)]
pub struct FmtSize {
  pub x: u32,
  pub y: u32
}

pub struct FmtContent {
  content: String,
  size: FmtSize,
}

impl FmtContent {
  fn new(content: String) -> FmtContent {
    let lines: Vec<&str> = content.split("\n").collect();
    let y = lines.len() as u32;
    let mut x = 0 as u32;

    for line in lines {
      x = x.max(line.len() as u32);
    }

    FmtContent {
      content,
      size: FmtSize { x, y }
    }
  }

  pub fn content(&self) -> &String {
    &self.content
  }
}

// horizontal preference
// when there's no perfect center
pub enum HP {
  L, R
}

// horizontal alignment
pub enum HAlign {
  Left,
  Center(HP),
  Right,
}

// vertical preference
// when there's no perfect middle
pub enum VP {
  U, D
}

// vertical alignment
pub enum VAlign {
  Top,
  Mid(VP),
  Bot,
}

pub struct FmtCell {
  content: Option<FmtContent>,
  pos: FmtPos,
  size: FmtSize,
  span_l: Vec<u32>,
  span_r: Vec<u32>,
  span_u: Vec<u32>,
  span_d: Vec<u32>,
  halign: HAlign,
  valign: VAlign,
}

fn sum(vec: &Vec<u32>) -> u32 {
  let mut sum = 0;
  for i in vec {
    sum += i;
  }
  sum
}

impl FmtCell {

  pub fn new(
    pos_x: i32, pos_y: i32,
    size_x: u32, size_y: u32
  ) -> FmtCell {
    FmtCell {
      content: None,
      pos: FmtPos { x: pos_x, y: pos_y },
      size: FmtSize { x: size_x, y: size_y },
      span_l: Vec::new(),
      span_r: Vec::new(),
      span_u: Vec::new(),
      span_d: Vec::new(),
      halign: HAlign::Left,
      valign: VAlign::Top,
    }
  }

  pub fn set_halign(&mut self, align: HAlign) {
    self.halign = align;
  }

  pub fn set_valign(&mut self, align: VAlign) {
    self.valign = align;
  }

  pub fn pos(&self) -> FmtPos { self.pos.clone() }
  pub fn size(&self) -> FmtSize { self.size.clone() }

  pub fn expand_l(&mut self, size: u32) {
    self.span_l.push(size);
  }

  pub fn expand_r(&mut self, size: u32) {
    self.span_r.push(size);
  }

  pub fn expand_u(&mut self, size: u32) {
    self.span_u.push(size);
  }

  pub fn expand_d(&mut self, size: u32) {
    self.span_d.push(size);
  }

  pub fn content(&self) -> &Option<FmtContent> {
    &self.content
  }

  pub fn span_l_vec(&self) -> &Vec<u32> {
    &self.span_l
  }

  pub fn span_r_vec(&self) -> &Vec<u32> {
    &self.span_r
  }

  pub fn span_u_vec(&self) -> &Vec<u32> {
    &self.span_u
  }

  pub fn span_d_vec(&self) -> &Vec<u32> {
    &self.span_d
  }

  pub fn span_l(&self) -> u32 {
    self.span_l.len() as u32
  }

  pub fn span_r(&self) -> u32 {
    self.span_r.len() as u32
  }

  pub fn span_u(&self) -> u32 {
    self.span_u.len() as u32
  }

  pub fn span_d(&self) -> u32 {
    self.span_d.len() as u32
  }

  pub fn set_content(&mut self, content: String)
  -> Result<(), String> {
    let content = FmtContent::new(content);

    let w = self.size.x + sum(&self.span_l) + sum(&self.span_r);
    
    if content.size.x > w {
      return Err(content.content);
    }

    let h = self.size.y + sum(&self.span_u) + sum(&self.span_d);

    if content.size.y > h {
      return Err(content.content);
    }

    self.content = Some(content);

    Ok(())
  }

  pub fn collision_check(&self, other: &FmtCell) -> bool {
    let mut h_col = false;
    let mut v_col = false;

    // self is left, other is right
    if self.pos.x < other.pos.x {
      if self.pos.x + self.span_r() as i32
      >= other.pos.x - other.span_l() as i32 {
        h_col = true;
      }
    } else { // other is left, self is right
      if other.pos.x + other.span_r() as i32
      >= self.pos.x - self.span_l() as i32 {
        h_col = true;
      }
    }

    // self is above, other is below
    if self.pos.y < other.pos.y {
      if self.pos.y + self.span_d() as i32
      >= other.pos.y - other.span_u() as i32 {
        v_col = true;
      }
    } else { // other is above, self is below
      if other.pos.y + other.span_d() as i32
      >= self.pos.y - self.span_u() as i32 {
        v_col = true;
      }
    }

    h_col && v_col
  }

  pub fn buffer(&self, x_size: u32, y_size: u32)
  -> Result<Vec<Vec<char>>, String> {
    let mut buffer: Vec<Vec<char>> = Vec::new();
    for _ in 0..y_size {
      buffer.push(
        vec![' '; x_size as usize]
      );
    }
    let string;

    if let Some(content) = &self.content {
      string = &content.content;
    } else { // None
      return Ok(buffer);
    }

    let lines: Vec<&str> = string.split("\n").collect();

    let content_h = lines.len() as u32;
    
    if content_h > y_size { 
      return Err(format!("y_size not enough")) 
    }

    let padding_h = y_size - content_h;

    let y_offset = match &self.valign {
      VAlign::Top => {
        0
      }
      VAlign::Mid(vp) => {
        let half_h = padding_h / 2;
        if padding_h % 2 == 0 {
          half_h
        } else {
          match vp {
            VP::U => {
              half_h
            }
            VP::D => {
              half_h + 1
            }
          }
        }
      }
      VAlign::Bot => {
        padding_h
      }
    }; // deciding y_offset

    // now need to write lines one by one

    let mut line_idx = 0;
    for line in lines {
      let chars: Vec<char> = line.chars().collect();
      let len = chars.len() as u32;

      if len > x_size { return Err(format!("x_size not enough")) }

      let padding_w = x_size - len;

      let x_offset = match &self.halign {
        HAlign::Left => {
          0
        }
        HAlign::Center(hp) => {
          let half_w = padding_w / 2;
          if padding_w % 2 == 0 {
            half_w
          } else {
            match hp {
              HP::L => {
                half_w
              }
              HP::R => {
                half_w + 1
              }
            }
          }
        }
        HAlign::Right => {
          padding_w
        }
      }; // deciding x_offset

      // now write the chars one by one

      let mut char_idx = 0;
      for char in chars {
        if let Some(line_mut) = buffer.get_mut(
          (y_offset + line_idx) as usize
        ) {
          if let Some(char_mut) = line_mut.get_mut(
            (x_offset + char_idx) as usize
          ) {
            *char_mut = char;
          } else {
            return Err(format!("char not found"));
          }
        } else {
          return Err(format!("line not found"));
        }
        char_idx += 1;
      }
      line_idx += 1;
    }

    Ok(buffer)
  }  

}