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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
#![deny(missing_docs)]
#![allow(clippy::return_self_not_must_use)]
//! Adaptive Card implementation
//!
//! [Webex Teams currently supports only version 1.1](https://developer.webex.com/docs/cards)
//!
//! More info about the schema can be found [here](https://adaptivecards.io/explorer/)

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Adaptive Card structure for message attachment
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct AdaptiveCard {
    /// Must be "AdaptiveCard"
    #[serde(rename = "type")]
    pub card_type: String,
    /// Schema version that this card requires. If a client is lower than this version, the fallbackText will be rendered.
    /// Maximum version is 1.1
    pub version: String,
    /// The card elements to show in the primary card region.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<Vec<CardElement>>,
    /// Actions available for this card
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actions: Option<Vec<Action>>,
    /// An Action that will be invoked when the card is tapped or selected.
    #[serde(rename = "selectAction", skip_serializing_if = "Option::is_none")]
    pub select_action: Option<Box<Action>>,
    /// Text shown when the client doesn’t support the version specified (may contain markdown).
    #[serde(rename = "fallbackText", skip_serializing_if = "Option::is_none")]
    pub fallback_text: Option<String>,
    /// Specifies the minimum height of the card.
    #[serde(rename = "minHeight", skip_serializing_if = "Option::is_none")]
    pub min_height: Option<String>,
    /// The 2-letter ISO-639-1 language used in the card. Used to localize any date/time functions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lang: Option<String>,
    /// The Adaptive Card schema.
    /// <http://adaptivecards.io/schemas/adaptive-card.json>
    #[serde(rename = "$schema")]
    pub schema: String,
}

impl AdaptiveCard {
    /// Create new adaptive card with mandatory defaults
    #[must_use]
    pub fn new() -> Self {
        Self {
            card_type: "AdaptiveCard".to_string(),
            version: "1.1".to_string(),
            body: None,
            actions: None,
            select_action: None,
            fallback_text: None,
            min_height: None,
            lang: None,
            schema: "http://adaptivecards.io/schemas/adaptive-card.json".to_string(),
        }
    }

    /// Adds Element to body
    ///
    /// # Arguments
    ///
    /// * `card` - `CardElement` to add
    pub fn add_body<T: Into<CardElement>>(&mut self, card: T) -> Self {
        //self.body = self.body.map_or_else(|| Some(vec![card.into()]), |body| body.push(card.into()));
        //self.body = Some(self.body.unwrap_or_default().push(card.into()));
        // TODO: improve this - can we use take()?
        self.body = Some(match self.body.clone() {
            None => {
                vec![card.into()]
            }
            Some(mut body) => {
                body.push(card.into());
                body
            }
        });
        self.into()
    }

    /// Adds Actions
    ///
    /// # Arguments
    ///
    /// * `action` - Action to add
    pub fn add_action<T: Into<Action>>(&mut self, a: T) -> Self {
        self.actions = Some(match self.actions.clone() {
            None => {
                vec![a.into()]
            }
            Some(mut action) => {
                action.push(a.into());
                action
            }
        });
        self.into()
    }
}

impl From<&Self> for AdaptiveCard {
    #[must_use]
    fn from(item: &Self) -> Self {
        item.clone()
    }
}

impl From<&mut Self> for AdaptiveCard {
    #[must_use]
    fn from(item: &mut Self) -> Self {
        item.clone()
    }
}

/// Card element types
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum CardElement {
    /// Containers group items together.
    Container {
        /// The card elements to render inside the Container.
        items: Vec<CardElement>,
        /// An Action that will be invoked when the Container is tapped or selected.
        #[serde(rename = "selectAction", skip_serializing_if = "Option::is_none")]
        select_action: Option<Action>,
        /// Style hint for Container.
        #[serde(skip_serializing_if = "Option::is_none")]
        style: Option<ContainerStyle>,
        /// Defines how the content should be aligned vertically within the container.
        #[serde(
            rename = "verticalContentAlignment",
            skip_serializing_if = "Option::is_none"
        )]
        vertical_content_alignment: Option<VerticalContentAlignment>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// A unique identifier associated with the item.
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// ColumnSet divides a region into Columns, allowing elements to sit side-by-side.
    ColumnSet {
        /// The array of Columns to divide the region into.
        columns: Vec<Column>,
        /// An Action that will be invoked when the ColumnSet is tapped or selected.
        #[serde(rename = "selectAction", skip_serializing_if = "Option::is_none")]
        select_action: Option<Action>,
        /// A unique identifier associated with the item.
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// The FactSet element displays a series of facts (i.e. name/value pairs) in a tabular form.
    FactSet {
        /// The array of Fact‘s.
        facts: Vec<Fact>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// A unique identifier associated with the item.
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// The ImageSet displays a collection of Images similar to a gallery.
    ImageSet {
        /// The array of Image elements to show.
        images: Vec<CardElement>,
        /// Controls the approximate size of each image. The physical dimensions will vary per host.
        #[serde(rename = "imageSize", skip_serializing_if = "Option::is_none")]
        image_size: Option<ImageSize>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// A unique identifier associated with the item.
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Displays text, allowing control over font sizes, weight, and color.
    TextBlock {
        /// Text to display
        text: String,
        /// If true, allow text to wrap. Otherwise, text is clipped.
        #[serde(skip_serializing_if = "Option::is_none")]
        wrap: Option<bool>,
        /// Controls the color of TextBlock elements.
        #[serde(skip_serializing_if = "Option::is_none")]
        color: Option<Color>,
        /// Controls the horizontal text alignment.
        #[serde(
            rename = "HorizontalAlignment",
            skip_serializing_if = "Option::is_none"
        )]
        horizontal_alignment: Option<HorizontalAlignment>,
        /// If true, displays text slightly toned down to appear less prominent.
        #[serde(rename = "isSubtle", skip_serializing_if = "Option::is_none")]
        is_subtle: Option<bool>,
        /// Specifies the maximum number of lines to display.
        #[serde(rename = "maxLines", skip_serializing_if = "Option::is_none")]
        max_lines: Option<u64>,
        /// Specifies the font type
        #[serde(rename = "fontType", skip_serializing_if = "Option::is_none")]
        font_type: Option<FontType>,
        /// Controls size of text.
        #[serde(skip_serializing_if = "Option::is_none")]
        size: Option<Size>,
        /// Controls the weight of TextBlock elements.
        #[serde(skip_serializing_if = "Option::is_none")]
        weight: Option<Weight>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// A unique identifier associated with the item.
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Displays an image.
    Image {
        /// The URL to the image.
        url: String,
        /// Alternate text describing the image.
        #[serde(rename = "altText", skip_serializing_if = "Option::is_none")]
        alt_text: Option<String>,
        /// Applies a background to a transparent image. This property will respect the image style.
        /// hex value of a color (e.g. #982374)
        #[serde(rename = "backgroundColor", skip_serializing_if = "Option::is_none")]
        background_color: Option<String>,
        /// The desired on-screen width of the image, ending in ‘px’. E.g., 50px. This overrides the size property.
        #[serde(skip_serializing_if = "Option::is_none")]
        width: Option<String>,
        /// The desired height of the image. If specified as a pixel value, ending in ‘px’, E.g., 50px, the image will distort to fit that exact height. This overrides the size property.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<String>,
        /// Controls how this element is horizontally positioned within its parent.
        #[serde(
            rename = "horizontalAlignment",
            skip_serializing_if = "Option::is_none"
        )]
        horizontal_alignment: Option<HorizontalAlignment>,
        /// An Action that will be invoked when the Image is tapped or selected. Action.ShowCard is not supported.
        #[serde(rename = "selectAction", skip_serializing_if = "Option::is_none")]
        select_action: Option<Action>,
        /// Controls the approximate size of the image. The physical dimensions will vary per host.
        #[serde(skip_serializing_if = "Option::is_none")]
        size: Option<ImageSize>,
        /// Controls how this Image is displayed.
        #[serde(skip_serializing_if = "Option::is_none")]
        style: Option<ImageStyle>,
        /// A unique identifier associated with the item.
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Lets a user enter text.
    #[serde(rename = "Input.Text")]
    InputText {
        /// Unique identifier for the value. Used to identify collected input when the Submit action is performed.
        id: String,
        /// Description of the input desired. Displayed when no text has been input.
        #[serde(skip_serializing_if = "Option::is_none")]
        placeholder: Option<String>,
        /// If true, allow multiple lines of input.
        #[serde(rename = "isMultiline", skip_serializing_if = "Option::is_none")]
        is_multiline: Option<bool>,
        /// Hint of maximum length characters to collect (may be ignored by some clients).
        #[serde(rename = "maxLength", skip_serializing_if = "Option::is_none")]
        max_length: Option<u64>,
        /// Text Input Style
        #[serde(skip_serializing_if = "Option::is_none")]
        style: Option<TextInputStyle>,
        /// The inline action for the input. Typically displayed to the right of the input.
        #[serde(rename = "inlineAction", skip_serializing_if = "Option::is_none")]
        inline_action: Option<Action>,
        /// The initial value for this field.
        #[serde(skip_serializing_if = "Option::is_none")]
        value: Option<String>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Allows a user to enter a number.
    #[serde(rename = "Input.Number")]
    InputNumber {
        /// Unique identifier for the value. Used to identify collected input when the Submit action is performed.
        id: String,
        /// Description of the input desired. Displayed when no selection has been made.
        #[serde(skip_serializing_if = "Option::is_none")]
        placeholder: Option<String>,
        /// Hint of maximum value (may be ignored by some clients).
        #[serde(skip_serializing_if = "Option::is_none")]
        max: Option<f64>,
        /// Hint of minimum value (may be ignored by some clients).
        #[serde(skip_serializing_if = "Option::is_none")]
        min: Option<f64>,
        /// Initial value for this field.
        #[serde(skip_serializing_if = "Option::is_none")]
        value: Option<f64>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Lets a user choose a date.
    #[serde(rename = "Input.Date")]
    InputDate {
        /// Unique identifier for the value. Used to identify collected input when the Submit action is performed.
        id: String,
        /// Description of the input desired. Displayed when no selection has been made.
        #[serde(skip_serializing_if = "Option::is_none")]
        placeholder: Option<String>,
        /// Hint of maximum value expressed in YYYY-MM-DD(may be ignored by some clients).
        #[serde(skip_serializing_if = "Option::is_none")]
        max: Option<String>,
        /// Hint of minimum value expressed in YYYY-MM-DD(may be ignored by some clients).
        #[serde(skip_serializing_if = "Option::is_none")]
        min: Option<String>,
        /// The initial value for this field expressed in YYYY-MM-DD.
        #[serde(skip_serializing_if = "Option::is_none")]
        value: Option<String>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Lets a user select a time.
    #[serde(rename = "Input.Time")]
    InputTime {
        /// Unique identifier for the value. Used to identify collected input when the Submit action is performed.
        id: String,
        /// Hint of maximum value expressed in HH:MM (may be ignored by some clients).
        #[serde(skip_serializing_if = "Option::is_none")]
        max: Option<String>,
        /// Hint of minimum value expressed in HH:MM (may be ignored by some clients).
        #[serde(skip_serializing_if = "Option::is_none")]
        min: Option<String>,
        /// The initial value for this field expressed in HH:MM.
        #[serde(skip_serializing_if = "Option::is_none")]
        value: Option<String>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Lets a user choose between two options.
    #[serde(rename = "Input.Toggle")]
    InputToggle {
        /// Unique identifier for the value. Used to identify collected input when the Submit action is performed.
        id: String,
        /// The initial selected value. If you want the toggle to be initially on, set this to the value of valueOn‘s value.
        #[serde(skip_serializing_if = "Option::is_none")]
        value: Option<String>,
        /// The value when toggle is off
        #[serde(rename = "valueOff", skip_serializing_if = "Option::is_none")]
        value_off: Option<String>,
        /// The value when toggle is on
        #[serde(rename = "valueOn", skip_serializing_if = "Option::is_none")]
        value_on: Option<String>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
    },

    /// Allows a user to input a Choice.
    #[serde(rename = "Input.ChoiceSet")]
    InputChoiceSet {
        /// Choice options.
        choices: Vec<Choice>,
        /// Unique identifier for the value. Used to identify collected input when the Submit action is performed.
        id: String,
        /// Allow multiple choices to be selected.
        #[serde(rename = "isMultiSelect", skip_serializing_if = "Option::is_none")]
        is_multi_select: Option<bool>,
        /// Input Choice Style
        #[serde(skip_serializing_if = "Option::is_none")]
        style: Option<ChoiceInputStyle>,
        /// The initial choice (or set of choices) that should be selected. For multi-select, specify a comma-separated string of values.
        #[serde(skip_serializing_if = "Option::is_none")]
        value: Option<String>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
        /// When true, draw a separating line at the top of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        separator: Option<bool>,
        /// Controls the amount of spacing between this element and the preceding element.
        #[serde(skip_serializing_if = "Option::is_none")]
        spacing: Option<Spacing>,
    },

    /// Displays a set of actions.
    ActionSet {
        /// The array of Action elements to show.
        actions: Vec<Action>,
        /// Specifies the height of the element.
        #[serde(skip_serializing_if = "Option::is_none")]
        height: Option<Height>,
    },
}

impl From<&Self> for CardElement {
    #[must_use]
    fn from(item: &Self) -> Self {
        item.clone()
    }
}

impl From<&mut Self> for CardElement {
    #[must_use]
    fn from(item: &mut Self) -> Self {
        item.clone()
    }
}

/// Functions for Card Element
impl CardElement {
    /// Create container
    #[must_use]
    pub const fn container() -> Self {
        Self::Container {
            items: vec![],
            select_action: None,
            style: None,
            vertical_content_alignment: None,
            height: None,
            id: None,
            separator: None,
            spacing: None,
        }
    }

    /// Add element to Container
    pub fn add_element<T: Into<Self>>(&mut self, element: T) -> Self {
        if let Self::Container { items, .. } = self {
            items.push(element.into());
        }
        self.into()
    }

    /// Set Container Style
    pub fn set_container_style(&mut self, s: ContainerStyle) -> Self {
        if let Self::Container { style, .. } = self {
            *style = Some(s);
        }
        self.into()
    }

    /// Set container contents vertical alignment
    pub fn set_vertical_alignment(&mut self, align: VerticalContentAlignment) -> Self {
        if let Self::Container {
            vertical_content_alignment,
            ..
        } = self
        {
            *vertical_content_alignment = Some(align);
        }
        self.into()
    }

    /// Create input.Text
    #[must_use]
    pub fn input_text<T: Into<String>, S: Into<String>>(id: T, value: Option<S>) -> Self {
        Self::InputText {
            id: id.into(),
            placeholder: None,
            is_multiline: None,
            max_length: None,
            style: None,
            inline_action: None,
            value: value.map(Into::into),
            height: None,
            separator: None,
            spacing: None,
        }
    }

    /// Set Text Input Multiline
    pub fn set_multiline(&mut self, s: bool) -> Self {
        if let Self::InputText { is_multiline, .. } = self {
            *is_multiline = Some(s);
        }
        self.into()
    }

    /// Create input.ChoiceSet
    #[must_use]
    pub fn input_choice_set<T: Into<String>, S: Into<String>>(id: T, value: Option<S>) -> Self {
        Self::InputChoiceSet {
            choices: vec![],
            id: id.into(),
            is_multi_select: None,
            style: None,
            value: value.map(Into::into),
            height: None,
            separator: None,
            spacing: None,
        }
    }

    /// Create input.Toggle
    #[must_use]
    pub fn input_toggle<T: Into<String>>(id: T, value: bool) -> Self {
        Self::InputToggle {
            id: id.into(),
            value: Some(value.to_string()),
            value_off: None,
            value_on: None,
            height: None,
            separator: None,
            spacing: None,
            title: None,
        }
    }

    /// Set choiceSet Style
    pub fn set_style(&mut self, s: ChoiceInputStyle) -> Self {
        if let Self::InputChoiceSet { style, .. } = self {
            *style = Some(s);
        }
        self.into()
    }

    /// Set title Style
    pub fn set_title(&mut self, s: String) -> Self {
        if let Self::InputToggle { title, .. } = self {
            *title = Some(s);
        }
        self.into()
    }

    /// Set choiceSet Style
    pub fn set_multiselect(&mut self, b: bool) -> Self {
        if let Self::InputChoiceSet {
            is_multi_select, ..
        } = self
        {
            *is_multi_select = Some(b);
        }
        self.into()
    }

    /// Create textBlock
    ///
    /// # Arguments
    ///
    /// * `text` - Text to set to the new text block (Must implement `Into<String>`)
    #[must_use]
    pub fn text_block<T: Into<String>>(text: T) -> Self {
        Self::TextBlock {
            text: text.into(),
            wrap: None,
            color: None,
            horizontal_alignment: None,
            is_subtle: None,
            max_lines: None,
            font_type: None,
            size: None,
            weight: None,
            height: None,
            id: None,
            separator: None,
            spacing: None,
        }
    }

    /// Set Text Weight
    pub fn set_weight(&mut self, w: Weight) -> Self {
        if let Self::TextBlock { weight, .. } = self {
            *weight = Some(w);
        }
        self.into()
    }

    /// Set Text Font Type
    pub fn set_font(&mut self, f: FontType) -> Self {
        if let Self::TextBlock { font_type, .. } = self {
            *font_type = Some(f);
        }
        self.into()
    }

    /// Set Text Size
    pub fn set_size(&mut self, s: Size) -> Self {
        if let Self::TextBlock { size, .. } = self {
            *size = Some(s);
        }
        self.into()
    }

    /// Set Text Color
    pub fn set_color(&mut self, c: Color) -> Self {
        if let Self::TextBlock { color, .. } = self {
            *color = Some(c);
        }
        self.into()
    }

    /// Set Text wrap
    pub fn set_wrap(&mut self, w: bool) -> Self {
        if let Self::TextBlock { wrap, .. } = self {
            *wrap = Some(w);
        }
        self.into()
    }

    /// Set Text subtle
    pub fn set_subtle(&mut self, s: bool) -> Self {
        if let Self::TextBlock { is_subtle, .. } = self {
            *is_subtle = Some(s);
        }
        self.into()
    }

    /// Create factSet
    #[must_use]
    pub const fn fact_set() -> Self {
        Self::FactSet {
            facts: vec![],
            height: None,
            id: None,
            separator: None,
            spacing: None,
        }
    }

    /// Create image
    pub fn image<T: Into<String>>(url: T) -> Self {
        Self::Image {
            url: url.into(),
            alt_text: None,
            background_color: None,
            width: None,
            height: None,
            horizontal_alignment: None,
            select_action: None,
            size: None,
            style: None,
            id: None,
            separator: None,
            spacing: None,
        }
    }

    /// Add fact to factSet
    pub fn add_key_value<T: Into<String>, S: Into<String>>(&mut self, title: T, value: S) -> Self {
        match self {
            Self::FactSet { facts, .. } => facts.push(Fact {
                title: title.into(),
                value: value.into(),
            }),
            Self::InputChoiceSet { choices, .. } => choices.push(Choice {
                title: title.into(),
                value: value.into(),
            }),
            _ => {
                log::warn!("Card does not have key value type field");
            }
        }
        self.into()
    }

    /// Create columnSet
    #[must_use]
    pub const fn column_set() -> Self {
        Self::ColumnSet {
            columns: vec![],
            select_action: None,
            id: None,
            separator: None,
            spacing: None,
        }
    }

    /// Add column to columnSet
    pub fn add_column(&mut self, column: Column) -> Self {
        if let Self::ColumnSet { columns, .. } = self {
            columns.push(column);
        }
        self.into()
    }

    /// Set Separator
    pub fn set_separator(&mut self, s: bool) -> Self {
        match self {
            Self::TextBlock { separator, .. }
            | Self::FactSet { separator, .. }
            | Self::ColumnSet { separator, .. }
            | Self::Image { separator, .. }
            | Self::InputChoiceSet { separator, .. }
            | Self::InputText { separator, .. }
            | Self::InputToggle { separator, .. } => {
                *separator = Some(s);
            }
            _ => {
                log::warn!("Card does not have separator field");
            }
        }
        self.into()
    }

    /// Set Placeholder
    pub fn set_placeholder(&mut self, s: Option<String>) -> Self {
        match self {
            Self::InputText { placeholder, .. }
            | Self::InputNumber { placeholder, .. }
            | Self::InputDate { placeholder, .. } => {
                *placeholder = s;
            }
            _ => {
                log::warn!("Card does not have placeholder field");
            }
        }
        self.into()
    }

    /// Set Spacing
    pub fn set_spacing(&mut self, s: Spacing) -> Self {
        match self {
            Self::TextBlock { spacing, .. }
            | Self::FactSet { spacing, .. }
            | Self::ColumnSet { spacing, .. }
            | Self::Image { spacing, .. }
            | Self::InputChoiceSet { spacing, .. }
            | Self::InputText { spacing, .. } => {
                *spacing = Some(s);
            }
            _ => {
                log::warn!("Card does not have spacing field");
            }
        }
        self.into()
    }

    /// Create actionSet
    #[must_use]
    pub const fn action_set() -> Self {
        Self::ActionSet {
            actions: vec![],
            height: None,
        }
    }

    /// Add action to actionSet
    pub fn add_action_to_set(&mut self, action: Action) -> Self {
        if let Self::ActionSet { actions, .. } = self {
            actions.push(action);
        }
        self.into()
    }
}

/// Defines a container that is part of a `ColumnSet`.
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct Column {
    /// The card elements to render inside the Column.
    items: Vec<CardElement>,
    /// An Action that will be invoked when the Column is tapped or selected.
    #[serde(rename = "selectAction", skip_serializing_if = "Option::is_none")]
    select_action: Option<Action>,
    /// Style hint for Column.
    #[serde(skip_serializing_if = "Option::is_none")]
    style: Option<ContainerStyle>,
    /// Defines how the content should be aligned vertically within the column.
    #[serde(
        rename = "verticalContentAlignment",
        skip_serializing_if = "Option::is_none"
    )]
    vertical_content_alignment: Option<VerticalContentAlignment>,
    /// When true, draw a separating line between this column and the previous column.
    #[serde(skip_serializing_if = "Option::is_none")]
    separator: Option<bool>,
    /// Controls the amount of spacing between this column and the preceding column.
    #[serde(skip_serializing_if = "Option::is_none")]
    spacing: Option<Spacing>,
    /// "auto", "stretch", a number representing relative width of the column in the column group, or in version 1.1 and higher, a specific pixel width, like "50px".
    #[serde(skip_serializing_if = "Option::is_none")]
    width: Option<serde_json::Value>,
    /// A unique identifier associated with the item.
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<String>,
}

impl From<&Self> for Column {
    #[must_use]
    fn from(item: &Self) -> Self {
        item.clone()
    }
}

impl From<&mut Self> for Column {
    #[must_use]
    fn from(item: &mut Self) -> Self {
        item.clone()
    }
}

impl Column {
    /// Creates new Column
    #[must_use]
    pub const fn new() -> Self {
        Self {
            items: vec![],
            select_action: None,
            style: None,
            vertical_content_alignment: None,
            separator: None,
            spacing: None,
            width: None,
            id: None,
        }
    }

    /// Adds element to column
    pub fn add_element(&mut self, item: CardElement) -> Self {
        self.items.push(item);
        self.into()
    }

    /// Sets separator
    pub fn set_separator(&mut self, s: bool) -> Self {
        self.separator = Some(s);
        self.into()
    }

    /// Sets `VerticalContentAlignment`
    pub fn set_vertical_alignment(&mut self, s: VerticalContentAlignment) -> Self {
        self.vertical_content_alignment = Some(s);
        self.into()
    }

    /// Sets width
    pub fn set_width<T: Into<String>>(&mut self, s: T) -> Self {
        self.width =  Some(serde_json::Value::String(s.into()));
        self.into()
    }
}

/// Describes a Fact in a `FactSet` as a key/value pair.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Fact {
    /// The title of the fact.
    title: String,
    /// The value of the fact.
    value: String,
}

/// Available color options
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum Color {
    Default,
    Dark,
    Light,
    Accent,
    Good,
    Warning,
    Attention,
}

/// Container Styles
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum ContainerStyle {
    Default,
    Emphasis,
    Good,
    Attention,
    Warning,
    Accent,
}

/// Spacing options
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum Spacing {
    Default,
    None,
    Small,
    Medium,
    Large,
    ExtraLarge,
    Padding,
}

/// Choice Input Style
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum ChoiceInputStyle {
    Compact,
    Expanded,
}

/// Vertical alignment of content
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum VerticalContentAlignment {
    Top,
    Center,
    Bottom,
}

/// Text Input Style
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum TextInputStyle {
    Text,
    Tel,
    Url,
    Email,
}

/// Height
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum Height {
    Auto,
    Stretch,
}

/// Image Style
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum ImageStyle {
    Default,
    Person,
}

/// Text Weight
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum Weight {
    Default,
    Lighter,
    Bolder,
}

/// Type of font to use for rendering
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum FontType {
    Default,
    Monospace,
}

/// Text Size
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum Size {
    Default,
    Small,
    Medium,
    Large,
    ExtraLarge,
}

/// Image Size
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum ImageSize {
    Auto,
    Stretch,
    Small,
    Medium,
    Large,
}

/// Controls how this element is horizontally positioned within its parent.
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum HorizontalAlignment {
    Left,
    Center,
    Right,
}

/// Available Card Actions
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum Action {
    /// Gathers input fields, merges with optional data field, and sends an event to the client. It is up to the client to determine how this data is processed. For example: With BotFramework bots, the client would send an activity through the messaging medium to the bot.
    #[serde(rename = "Action.Submit")]
    Submit {
        /// Initial data that input fields will be combined with. These are essentially ‘hidden’ properties.
        #[serde(skip_serializing_if = "Option::is_none")]
        data: Option<HashMap<String, String>>,
        /// Label for button or link that represents this action.
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Controls the style of an Action, which influences how the action is displayed, spoken, etc.
        #[serde(skip_serializing_if = "Option::is_none")]
        style: Option<ActionStyle>,
    },
    /// When invoked, show the given url either by launching it in an external web browser or showing within an embedded web browser.
    #[serde(rename = "Action.OpenUrl")]
    OpenUrl {
        /// The URL to open.
        url: String,
        /// Label for button or link that represents this action.
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Controls the style of an Action, which influences how the action is displayed, spoken, etc.
        #[serde(skip_serializing_if = "Option::is_none")]
        style: Option<ActionStyle>,
    },
    /// Defines an AdaptiveCard which is shown to the user when the button or link is clicked.
    #[serde(rename = "Action.ShowCard")]
    ShowCard {
        /// The Adaptive Card to show.
        card: AdaptiveCard,
        /// Label for button or link that represents this action.
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Controls the style of an Action, which influences how the action is displayed, spoken, etc.
        #[serde(skip_serializing_if = "Option::is_none")]
        style: Option<ActionStyle>,
    },
}

/// Controls the style of an Action, which influences how the action is displayed, spoken, etc.
#[allow(missing_docs)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum ActionStyle {
    /// Action is displayed as normal
    Default,
    /// Action is displayed with a positive style (typically the button becomes accent color)
    Positive,
    /// Action is displayed with a destructive style (typically the button becomes red)
    Destructive,
}

/// Describes a choice for use in a `ChoiceSet`.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Choice {
    /// Text to display.
    pub title: String,
    /// The raw value for the choice. **NOTE:** do not use a , in the value, since a ChoiceSet with isMultiSelect set to true returns a comma-delimited string of choice values.
    pub value: String,
}