using FlutterCodeGenerator.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FlutterCodeGenerator.ModelTypes { internal class UserDefinedDerivedModelType : ComplexModelType { public ModelType BaseType; public UserDefinedDerivedModelType(Type type, string name) : base(type, name) { BaseType = ModelTypeGenerator.Create(type.BaseType, ""); if (!(BaseType is UserDefinedGenericModelType)) { var properties = type.GetProperties(); foreach (var property in properties) { var child = ModelTypeGenerator.Create(property.PropertyType, property.Name); Children.Add(child); } } } public override string GetFlutterTypeName(bool isDefault = true, bool isGenericName = false, bool isSingle = false) { if (Index > 1) { return $"{ParameterType.Name}{Index}"; } else { return $"{ParameterType.Name}"; } } public override string GetDartString() { var dartString = new StringBuilder(); dartString.AppendLine($"class {GetFlutterTypeName()} extends {BaseType.GetFlutterTypeName()}{{"); var baseType = BaseType as ComplexModelType; foreach (var child in Children) { if (baseType != null) { if (!baseType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper)) { var questionMark = QuestionMarkCheck(child); if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType) { dartString.AppendLine($"\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower};"); } else { dartString.AppendLine($"\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower};"); } } else { continue; } } else { var questionMark = QuestionMarkCheck(child); if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType) { dartString.AppendLine($"\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower};"); } else { dartString.AppendLine($"\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower};"); } } } dartString.AppendLine(); if (Children.Count == 0) { dartString.AppendLine($"\t{GetFlutterTypeName()}("); if (baseType != null && !(baseType is UserDefinedGenericModelType)) { dartString.AppendLine($"\t) : super("); foreach (var child in baseType.Children) { dartString.AppendLine($"\t\t\t{child.Name_Lower}: {child.Name_Lower},"); } dartString.AppendLine($"\t\t);"); } else { dartString.AppendLine("\t);"); } } else { dartString.AppendLine($"\t{GetFlutterTypeName()}({{"); foreach (var child in Children) { if (baseType != null) { if (baseType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper)) { var questionMark = QuestionMarkCheck(child); if (child.DefaultValue != null) { if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType) { dartString.AppendLine($"\t\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower} = {child.DefaultValue},"); } else { dartString.AppendLine($"\t\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower} = {child.DefaultValue},"); } } else { if (child is ListModelType || child is DictionaryModelType || child is UserDefinedGenericModelType) { dartString.AppendLine($"\t\t{child.GetFlutterTypeName(false, false)}{questionMark} {child.Name_Lower},"); } else { dartString.AppendLine($"\t\t{child.GetFlutterTypeName()}{questionMark} {child.Name_Lower},"); } } } else { if (child.DefaultValue != null) { dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},"); } else { dartString.AppendLine($"\t\tthis.{child.Name_Lower},"); } } } else { if (child.DefaultValue != null) { dartString.AppendLine($"\t\tthis.{child.Name_Lower} = {child.DefaultValue},"); } else { dartString.AppendLine($"\t\tthis.{child.Name_Lower},"); } } } if (baseType != null) { dartString.AppendLine($"\t}}) : super("); foreach (var child in baseType.Children) { dartString.AppendLine($"\t\t\t{child.Name_Lower}: {child.Name_Lower},"); } dartString.AppendLine($"\t\t);"); } else { dartString.AppendLine("\t});"); } } dartString.AppendLine(); dartString.Append(FromJson(Children)); dartString.Append(ToJson(Children)); dartString.AppendLine("}"); return dartString.ToString(); } protected override string ToJson(List Children) { var source = new StringBuilder(); source.AppendLine("\tMap toJson() {"); source.AppendLine("\t\tfinal map = super.toJson();"); foreach (var child in Children) { if (BaseType is ComplexModelType complexModelType) { if (!complexModelType.Children.Any(x => x.ParameterType == child.ParameterType && x.Name_Upper == child.Name_Upper)) { if (child is EnumModelType) { source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower}.index;"); } else if (child.DefaultValue != null) { source.AppendLine($"\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};"); } else if (child is DateTimeModelType) { source.AppendLine($"\t\tif({child.Name_Lower} != null)"); source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = JsonRpcUtils.dateFormat({child.Name_Lower}!);"); } else { source.AppendLine($"\t\tif({child.Name_Lower} != null)"); source.AppendLine($"\t\t\tmap['{child.Name_Upper}'] = {child.Name_Lower};"); } } } } source.AppendLine("\t\treturn map;"); source.AppendLine("\t}"); return source.ToString(); } } }