import 'dart:convert';

import 'package:fis_common/logger/logger.dart';
import 'package:flutter/services.dart';

/// 应用参数(随编译引入)
class AppParameters {
  static const _path = "assets/app_params.json";
  static late final AppParameterDataModel _data;
  static bool _hasLoaded = false;

  /// 数据内容
  static AppParameterDataModel get data => _data;

  /// 加载预置参数数据
  static Future<void> load() async {
    if (_hasLoaded) return;
    try {
      final dynamic jsonMap;
      final json = await rootBundle.loadString(_path, cache: false);
      jsonMap = jsonDecode(json);
      _data = AppParameterDataModel.fromJson(jsonMap);
    } catch (e) {
      logger.e("Load AppConfig error.", e);
      _data = AppParameterDataModel();
    }
    _hasLoaded = true;
  }
}

class AppParameterDataModel {
  // ignore: constant_identifier_names
  static const C_DEFAULT_GATEWAY = "http://192.168.6.80:8400"; // TODO:

  AppParameterDataModel({
    this.serverGateway = C_DEFAULT_GATEWAY,
  });
  factory AppParameterDataModel.fromJson(Map<String, dynamic> map) {
    return AppParameterDataModel(
      serverGateway: map['server_gateway'] ?? C_DEFAULT_GATEWAY,
    );
  }

  String serverGateway;
}
