using FlutterCodeGenerator.Model; using FlutterCodeGenerator.ModelTypes; using System; using System.Collections.Generic; using System.Linq; namespace FlutterCodeGenerator.Helper { internal class ModelTypeGenerator { public static Dictionary _tempTypes = new Dictionary(); public static ModelType Create(Type type, string argName, bool clearTypes = false) { if (clearTypes) { _tempTypes.Clear(); } GenerateDataCache.Instance.CurrentParameter = argName; var usedComplexModelTypeList = GenerateDataCache.Instance.GetCurrentServiceMap().UsedComplexModelTypeList; var conflictModelTypeList = GenerateDataCache.Instance.ConflictModelTypeList; if ((_tempTypes.ContainsKey(type) && _tempTypes[type] < 20) || !_tempTypes.ContainsKey(type)) { var duplicatedTime = 0; if (_tempTypes.ContainsKey(type)) { duplicatedTime = _tempTypes[type]; } duplicatedTime++; if (type == typeof(int) || type == typeof(long) || type == typeof(short) || type == typeof(uint) || type == typeof(ulong) || type == typeof(ushort)) { return new IntModelType(argName); } else if (type == typeof(int?) || type == typeof(long?) || type == typeof(short?) || type == typeof(uint?) || type == typeof(ulong?) || type == typeof(ushort?)) { return new IntNullableModelType(argName); } else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal)) { return new DoubleModelType(argName); } else if (type == typeof(float?) || type == typeof(double?) || type == typeof(decimal?)) { return new DoubleNullableModelType(argName); } else if (type == typeof(bool)) { return new BoolModelType(argName); } else if (type == typeof(bool?)) { return new BoolNullableModelType(argName); } else if (type == typeof(string) || type == typeof(char) || type.BaseType?.Name == "BaseParamsString" || type.BaseType?.Name == "BaseString") { return new StringModelType(argName); } else if (type == typeof(DateTime) || type == typeof(DateTime?)) { return new DateTimeModelType(argName); } else if (type == typeof(byte)) { return new ByteModelType(argName); } else if (type == typeof(byte?)) { return new ByteNullableModelType(argName); } else if (type == typeof(void)) { return new VoidModelType(argName); } else if (type.IsEnum) { var modelType = new EnumModelType(type, argName); if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name)) { conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary() { { modelType, 1 } }; } else { if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1; modelType.Index = index; conflictModelTypeList[modelType.ParameterType.Name][modelType] = index; } else { modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; } } if (!usedComplexModelTypeList.Any(x => x.ParameterType == type)) { usedComplexModelTypeList.Add(modelType); } return modelType; } else if (type.IsArray) { var modelType = new ArrayModelType(type, argName); return modelType; } else if (type.IsGenericType) { if (type.Name == "List`1" || type.Name == "IList`1" || type.Name == "IEnumerable`1" || type.Name == "IReadOnlyList`1") { var modelType = new ListModelType(type, argName); return modelType; } else if (type.Name == "Dictionary`2" || type.Name == "IDictionary`2") { var modelType = new DictionaryModelType(type, argName); return modelType; } else if (type.Name == "Nullable`1") { var modelType = new EnumNullableModelType(type.GetGenericArguments()[0], argName); if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name)) { conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary() { { modelType, 1 } }; } else { if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1; modelType.Index = index; conflictModelTypeList[modelType.ParameterType.Name][modelType] = index; } else { modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; } } if (!usedComplexModelTypeList.Any(x => x.ParameterType == type)) { usedComplexModelTypeList.Add(modelType); } return modelType; } else { _tempTypes[type] = duplicatedTime; var modelType = new UserDefinedGenericModelType(type, argName); if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name)) { conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary() { { modelType, 1 } }; } else { if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1; modelType.Index = index; conflictModelTypeList[modelType.ParameterType.Name][modelType] = index; } else { modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; } } if (!usedComplexModelTypeList.Any(x => x.ParameterType == type)) { usedComplexModelTypeList.Add(modelType); } return modelType; } } else if (type == typeof(object)) { throw new Exception("Object is not supported, it should be specific type."); } else if (type.BaseType != typeof(object) && type.BaseType != null) { _tempTypes[type] = duplicatedTime; var modelType = new UserDefinedDerivedModelType(type, argName); if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name)) { conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary() { { modelType, 1 } }; } else { if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1; modelType.Index = index; conflictModelTypeList[modelType.ParameterType.Name][modelType] = index; } else { modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; } } if (!usedComplexModelTypeList.Any(x => x.ParameterType == type)) { usedComplexModelTypeList.Add(modelType); } return modelType; } else if (type.IsInterface) { var modelType = new InterfaceModelType(argName); return modelType; } else { _tempTypes[type] = duplicatedTime; var modelType = new UserDefinedModelType(type, argName); if (!conflictModelTypeList.ContainsKey(modelType.ParameterType.Name)) { conflictModelTypeList[modelType.ParameterType.Name] = new Dictionary() { { modelType, 1 } }; } else { if (!conflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { var index = conflictModelTypeList[modelType.ParameterType.Name].Count() + 1; modelType.Index = index; conflictModelTypeList[modelType.ParameterType.Name][modelType] = index; } else { modelType.Index = conflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; } } if (!usedComplexModelTypeList.Any(x => x.ParameterType == type)) { usedComplexModelTypeList.Add(modelType); } return modelType; } } else { var modelType = new ExtraObjectModelType(type, argName); return modelType; } } } }