import 'dart:io';
import 'dart:convert';
import 'package:package_info_plus/package_info_plus.dart';
import '../../utils/kr_log_util.dart';

/// 配置数据模型
/// 用于存储应用程序的基础配置信息，包括加密信息、域名、启动图、官方联系方式等
class KRConfigData {
  /// 配置信息
  final String kr_config;

  /// 加密密钥
  final String kr_encryption_key;

  /// 加密方法
  final String kr_encryption_method;

  /// 可用域名列表
  final List<String> kr_domains;

  /// 启动页图片URL
  final String kr_startup_picture;

  /// 启动页跳过等待时间（秒）
  final int kr_startup_picture_skip_time;

  /// 应用更新信息
  final KRUpdateApplication kr_update_application;

  /// 官方邮箱
  final String kr_official_email;

  /// 官方网站
  final String kr_official_website;

  /// 官方电报群
  final String kr_official_telegram;

  /// 官方电话
  final String kr_official_telephone;

  /// 邀请链接
  final String kr_invitation_link;


  KRConfigData({
    this.kr_config = '',
    this.kr_encryption_key = '',
    this.kr_encryption_method = '',
    List<String>? kr_domains,
    this.kr_startup_picture = '',
    this.kr_startup_picture_skip_time = 0,
    KRUpdateApplication? kr_update_application,
    this.kr_official_email = '',
    this.kr_official_website = '',
    this.kr_official_telegram = '',
    this.kr_official_telephone = '',
    this.kr_invitation_link = '',
  })  : this.kr_domains = kr_domains ?? [],
        this.kr_update_application =
            kr_update_application ?? KRUpdateApplication();

  factory KRConfigData.fromJson(Map<String, dynamic> json) {
    KRLogUtil.kr_i('配置数据: $json', tag: 'KRConfigData');
    String _krConfigString = '';
    try {
      _krConfigString = jsonEncode(json);
    } catch (_) {
      _krConfigString = '';
    }
    return KRConfigData(
      kr_config: _krConfigString,
      kr_invitation_link: json['invitation_link'] ?? '',
      kr_encryption_key: json['encryption_key'] ?? '',
      kr_encryption_method: json['encryption_method'] ?? '',
      kr_domains: List<String>.from(json['domains'] ?? []),
      kr_startup_picture: json['startup_picture'] ?? '',
      kr_startup_picture_skip_time: json['startup_picture_skip_time'] ?? 0,
      kr_update_application:
          KRUpdateApplication.fromJson(json['applications'] ?? {}),
      kr_official_email: json['official_email'] ?? '',
      kr_official_website: json['official_website'] ?? '',
      kr_official_telegram: json['official_telegram'] ?? '',
      kr_official_telephone: json['official_telephone'] ?? '',
    );
  }
}

/// 应用更新信息模型
/// 用于存储应用程序的更新相关信息，包括版本号、下载地址等
class KRUpdateApplication {
  /// 应用ID
  final int kr_id;

  /// 应用名称
  final String kr_name;

  /// 应用描述
  final String kr_description;

  /// 应用下载地址
  final String kr_url;

  /// 应用版本号
  final String kr_version;

  /// 版本更新说明
  final String kr_version_description;

  /// 是否为默认应用
  final bool kr_is_default;

  final String kr_version_review;

  KRUpdateApplication({
    this.kr_id = 0,
    this.kr_name = '',
    this.kr_description = '',
    this.kr_url = '',
    this.kr_version = '',
    this.kr_version_description = '',
    this.kr_is_default = false,
    this.kr_version_review = '',
  });

  factory KRUpdateApplication.fromJson(Map<String, dynamic> json) {
    return KRUpdateApplication(
      kr_id: json['id'] ?? 0,
      kr_name: json['name'] ?? '',
      kr_description: json['description'] ?? '',
      kr_url: json['url'] ?? '',
      kr_version: json['version'] ?? '',
      kr_version_description: json['version_description'] ?? '',
      kr_is_default: json['is_default'] ?? false,
      kr_version_review: json['version_review'] ?? '',
    );
  }

  Future<bool> kr_is_daytime() async {
    if (Platform.isIOS) {
      if (kr_version_review.isNotEmpty) {
        // 获取当前应用版本号
        final PackageInfo packageInfo = await PackageInfo.fromPlatform();
        final String currentVersion = packageInfo.version;
        
        // 比较版本号
        return !(currentVersion == kr_version_review);
      }
      return true;
    }
    return true;
  }
}
