class EmbyAuthHeader {


  static String build({
    required String deviceId,
    required String deviceName,
    String? token,
    String? userId,
  }) {
    final parts = <String>[];
    if (token != null) {
      parts.add('Token="$token"');
    }
    if (userId != null && userId.isNotEmpty) {
      parts.add('Emby UserId="$userId"');
    }
    parts.addAll([
      'Client="${EmbyRequestHeaders.clientName}"',
      'Device="$deviceName"',
      'DeviceId="$deviceId"',
      'Version="${EmbyRequestHeaders.version}"',
    ]);
    return 'MediaBrowser ${parts.join(', ')}';
  }
}

class EmbyRequestHeaders {
  static const version = '6.1.0';
  static const clientName = 'SenPlayer';


  static const userAgent = 'SenPlayer/6.2.0';
  static const acceptLanguage = 'zh-CN,zh-Hans;q=0.9';
  static const embyLanguage = 'zh-cn';

  static Map<String, String> json({
    required String deviceId,
    required String deviceName,
    String? token,
    String? userId,
    bool hasBody = false,
  }) {
    return {
      'Accept': 'application/json',
      'Accept-Language': acceptLanguage,
      'User-Agent': userAgent,
      ..._clientIdentity(deviceId: deviceId, deviceName: deviceName),
      ..._auth(
        deviceId: deviceId,
        deviceName: deviceName,
        token: token,
        userId: userId,
      ),
      if (hasBody) 'Content-Type': 'application/json',
    };
  }

  static Map<String, String> image({
    required String deviceId,
    required String deviceName,
    required String token,
    required String userId,
  }) {
    return {
      'User-Agent': userAgent,
      'Accept': 'image/*,*/*;q=0.8',
      'Accept-Language': acceptLanguage,
      'Priority': 'u=3, i',
      ..._clientIdentity(deviceId: deviceId, deviceName: deviceName),
      ..._auth(
        deviceId: deviceId,
        deviceName: deviceName,
        token: token,
        userId: userId,
      ),
    };
  }

  static Map<String, String> media({
    required String deviceId,
    required String deviceName,
    required String token,
    required String userId,
  }) {
    return {
      'User-Agent': userAgent,
      'Accept': '*/*',
      'Accept-Language': acceptLanguage,
      ..._clientIdentity(deviceId: deviceId, deviceName: deviceName),
      ..._auth(
        deviceId: deviceId,
        deviceName: deviceName,
        token: token,
        userId: userId,
      ),
    };
  }


  static Map<String, String> directMedia() {
    return {
      'User-Agent': userAgent,
      'Accept': '*/*',
      'Accept-Language': acceptLanguage,
    };
  }

  static Map<String, String> _clientIdentity({
    required String deviceId,
    required String deviceName,
  }) {
    return {
      'X-Emby-Client': clientName,
      'X-Emby-Client-Version': version,
      'X-Emby-Device-Id': deviceId,
      'X-Emby-Device-Name': deviceName,
      'X-Emby-Language': embyLanguage,
    };
  }

  static Map<String, String> _auth({
    required String deviceId,
    required String deviceName,
    String? token,
    String? userId,
  }) {
    final authorizationHeader = EmbyAuthHeader.build(
      deviceId: deviceId,
      deviceName: deviceName,
      token: token,
      userId: userId,
    );
    return {
      'X-Emby-Authorization': authorizationHeader,
      'Authorization': authorizationHeader,
      if (token != null && token.isNotEmpty) ...{
        'X-Emby-Token': token,
        'X-MediaBrowser-Token': token,
      },
    };
  }
}
