using FlutterCodeGenerator.Helper; using FlutterCodeGenerator.Map.Interface; using FlutterCodeGenerator.Model; using FlutterCodeGenerator.ModelTypes; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FlutterCodeGenerator.Map { internal class OtherServiceMap : IServiceMap { private List _typeList; public string ServiceName { get; private set; } public List UsedComplexModelTypeList { get; } public OtherServiceMap(List types) { ServiceName = "OtherService"; _typeList = types; UsedComplexModelTypeList = new List(); GenerateDataCache.Instance.SetCurrentServiceMap(this); foreach (var type in _typeList) { try { var parameterModelType = ModelTypeGenerator.Create(type, type.Name, true); } catch (Exception ex) { Console.WriteLine($"Type is {type.FullName},Error:{ex}"); throw; } } } public string GetServiceDartString() { return null; } public string GetServiceModelDartString() { var dartString = new StringBuilder(); var importServiceList = new List(); var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList; foreach (var modelType in UsedComplexModelTypeList) { if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { if (modelType is EnumModelType enumModelType) { if (enumModelType.UserDefinedEnumDictionary.Any(x => x.Key >= 1000)) { continue; } else { alreadyGeneratedList.Add(modelType, ServiceName); dartString.AppendLine(modelType.GetDartString()); } } else { alreadyGeneratedList.Add(modelType, ServiceName); dartString.AppendLine(modelType.GetDartString()); } } else { var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != ServiceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } } if (string.IsNullOrWhiteSpace(dartString.ToString())) { return null; } var serviceModelDartString = new StringBuilder(); foreach (var importService in importServiceList) { serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';"); } if (importServiceList.Count > 0) { serviceModelDartString.AppendLine(); } var dartStringInfo = dartString.ToString(); if (dartStringInfo.Contains("JsonRpcUtils")) { serviceModelDartString.AppendLine(CommonParameters.StringUtils); serviceModelDartString.AppendLine(); } if (dartStringInfo.Contains("FJsonConvert")) { serviceModelDartString.AppendLine(CommonParameters.StringJsonConvert); serviceModelDartString.AppendLine(); } serviceModelDartString.AppendLine(dartStringInfo); return serviceModelDartString.ToString(); } } }