import 'dart:ui';

import 'package:fis_ui/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:universal_html/html.dart' as html;

///获取参数类
class ParameterHelper {
  ///参数集，第二窗口使用
  static Map<String, String> parasmeters = {};

  ///Url带的参数
  static String searchUrl = '';

  ///获取App启动参数
  static Map<String, String> getAppStartParameters() {
    Map<String, String> map = {};
    if (kIsMobile) {
      return map;
    }
    var url = html.window.location.search ?? '';

    if (url.contains('?')) {
      final parameters = url.split('?');
      if (parameters.length > 1) {
        final parameterStr = parameters[1];
        final keyValues = parameterStr.split('&');
        for (var kv in keyValues) {
          final keyValue = kv.split('=');
          map[keyValue[0]] = keyValue[1];
        }
        if (map.length > 0) {
          parasmeters = map;
          searchUrl = url;
          html.window.history.replaceState('', '', url);
          var lagCode = map['lagCode'] ?? '';
          if (lagCode.isNotEmpty) {
            var lans = lagCode.split('-');
            var newLocale = Locale(lans[0], lans[1]);
            if (Store.app.locale != newLocale) {
              Store.app.updateLocale(newLocale);
            }
          }
          return map;
        }
      }
    }
    return {};
  }

  static setWindowState() {
    html.window.history.replaceState('', '', searchUrl);
    print('setWindowState href:${html.window.location.href}');
  }
}
