// ignore_for_file: non_constant_identifier_names
// ignore_for_file: camel_case_types
// ignore_for_file: prefer_single_quotes

// This file is automatically generated. DO NOT EDIT, all your changes would be lost.
import 'package:flutter/material.dart' show debugPrint;
import 'package:anchor_collect_flutter/api/api_response.dart';
import 'package:anchor_collect_flutter/models/cattle_resume_entity.dart';
import 'package:anchor_collect_flutter/models/dict_entity.dart';
import 'package:anchor_collect_flutter/models/login_entity.dart';
import 'package:anchor_collect_flutter/models/message.dart';
import 'package:anchor_collect_flutter/models/user_model.dart';

JsonConvert jsonConvert = JsonConvert();

typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
typedef EnumConvertFunction<T> = T Function(String value);
typedef ConvertExceptionHandler = void Function(Object error, StackTrace stackTrace);

class JsonConvert {
  static ConvertExceptionHandler? onError;
  JsonConvertClassCollection convertFuncMap = JsonConvertClassCollection();

  /// When you are in the development, to generate a new model class, hot-reload doesn't find new generation model class, you can build on MaterialApp method called jsonConvert. ReassembleConvertFuncMap (); This method only works in a development environment
  /// https://flutter.cn/docs/development/tools/hot-reload
  /// class MyApp extends StatelessWidget {
  ///    const MyApp({Key? key})
  ///        : super(key: key);
  ///
  ///    @override
  ///    Widget build(BuildContext context) {
  ///      jsonConvert.reassembleConvertFuncMap();
  ///      return MaterialApp();
  ///    }
  /// }
  void reassembleConvertFuncMap() {
    bool isReleaseMode = const bool.fromEnvironment('dart.vm.product');
    if (!isReleaseMode) {
      convertFuncMap = JsonConvertClassCollection();
    }
  }

  T? convert<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
    if (value == null) {
      return null;
    }
    if (value is T) {
      return value;
    }
    try {
      return _asT<T>(value, enumConvert: enumConvert);
    } catch (e, stackTrace) {
      debugPrint('asT<$T> $e $stackTrace');
      if (onError != null) {
        onError!(e, stackTrace);
      }
      return null;
    }
  }

  List<T?>? convertList<T>(List<dynamic>? value, {EnumConvertFunction? enumConvert}) {
    if (value == null) {
      return null;
    }
    try {
      return value.map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)).toList();
    } catch (e, stackTrace) {
      debugPrint('asT<$T> $e $stackTrace');
      if (onError != null) {
        onError!(e, stackTrace);
      }
      return <T>[];
    }
  }

  List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
    if (value == null) {
      return null;
    }
    try {
      return (value as List<dynamic>).map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)!).toList();
    } catch (e, stackTrace) {
      debugPrint('asT<$T> $e $stackTrace');
      if (onError != null) {
        onError!(e, stackTrace);
      }
      return <T>[];
    }
  }

  T? _asT<T extends Object?>(dynamic value,
      {EnumConvertFunction? enumConvert}) {
    final String type = T.toString();
    final String valueS = value.toString();
    if (enumConvert != null) {
      return enumConvert(valueS) as T;
    } else if (type == "String") {
      return valueS as T;
    } else if (type == "int") {
      final int? intValue = int.tryParse(valueS);
      if (intValue == null) {
        return double.tryParse(valueS)?.toInt() as T?;
      } else {
        return intValue as T;
      }
    } else if (type == "double") {
      return double.parse(valueS) as T;
    } else if (type == "DateTime") {
      return DateTime.parse(valueS) as T;
    } else if (type == "bool") {
      if (valueS == '0' || valueS == '1') {
        return (valueS == '1') as T;
      }
      return (valueS == 'true') as T;
    } else if (type == "Map" || type.startsWith("Map<")) {
      return value as T;
    } else {
      if (convertFuncMap.containsKey(type)) {
        if (value == null) {
          return null;
        }
        return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T;
      } else {
        throw UnimplementedError('$type unimplemented,you can try running the app again');
      }
    }
  }

  //list is returned by type
  static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
    if (<ApiResponse>[] is M) {
      return data.map<ApiResponse>((Map<String, dynamic> e) => ApiResponse.fromJson(e)).toList() as M;
    }
    if (<ApiResponseData>[] is M) {
      return data.map<ApiResponseData>((Map<String, dynamic> e) => ApiResponseData.fromJson(e)).toList() as M;
    }
    if (<CattleResumeEntity>[] is M) {
      return data.map<CattleResumeEntity>((Map<String, dynamic> e) => CattleResumeEntity.fromJson(e)).toList() as M;
    }
    if (<DictEntity>[] is M) {
      return data.map<DictEntity>((Map<String, dynamic> e) => DictEntity.fromJson(e)).toList() as M;
    }
    if (<LoginEntity>[] is M) {
      return data.map<LoginEntity>((Map<String, dynamic> e) => LoginEntity.fromJson(e)).toList() as M;
    }
    if (<MessageModel>[] is M) {
      return data.map<MessageModel>((Map<String, dynamic> e) => MessageModel.fromJson(e)).toList() as M;
    }
    if (<UserModel>[] is M) {
      return data.map<UserModel>((Map<String, dynamic> e) => UserModel.fromJson(e)).toList() as M;
    }
    if (<UserModelParams>[] is M) {
      return data.map<UserModelParams>((Map<String, dynamic> e) => UserModelParams.fromJson(e)).toList() as M;
    }
    if (<UserModelDept>[] is M) {
      return data.map<UserModelDept>((Map<String, dynamic> e) => UserModelDept.fromJson(e)).toList() as M;
    }
    if (<UserModelDeptParams>[] is M) {
      return data.map<UserModelDeptParams>((Map<String, dynamic> e) => UserModelDeptParams.fromJson(e)).toList() as M;
    }
    if (<UserModelRoles>[] is M) {
      return data.map<UserModelRoles>((Map<String, dynamic> e) => UserModelRoles.fromJson(e)).toList() as M;
    }
    if (<UserModelRolesParams>[] is M) {
      return data.map<UserModelRolesParams>((Map<String, dynamic> e) => UserModelRolesParams.fromJson(e)).toList() as M;
    }

    debugPrint("${M.toString()} not found");

    return null;
  }

  static M? fromJsonAsT<M>(dynamic json) {
    if (json is M) {
      return json;
    }
    if (json is List) {
      return _getListChildType<M>(json.map((e) => e as Map<String, dynamic>).toList());
    } else {
      return jsonConvert.convert<M>(json);
    }
  }
}

class JsonConvertClassCollection {
  Map<String, JsonConvertFunction> convertFuncMap = {
    (ApiResponse).toString(): ApiResponse.fromJson,
    (ApiResponseData).toString(): ApiResponseData.fromJson,
    (CattleResumeEntity).toString(): CattleResumeEntity.fromJson,
    (DictEntity).toString(): DictEntity.fromJson,
    (LoginEntity).toString(): LoginEntity.fromJson,
    (MessageModel).toString(): MessageModel.fromJson,
    (UserModel).toString(): UserModel.fromJson,
    (UserModelParams).toString(): UserModelParams.fromJson,
    (UserModelDept).toString(): UserModelDept.fromJson,
    (UserModelDeptParams).toString(): UserModelDeptParams.fromJson,
    (UserModelRoles).toString(): UserModelRoles.fromJson,
    (UserModelRolesParams).toString(): UserModelRolesParams.fromJson,
  };

  bool containsKey(String type) {
    return convertFuncMap.containsKey(type);
  }

  JsonConvertFunction? operator [](String key) {
    return convertFuncMap[key];
  }
}