// Author: fengshengxiong
// Date: 2024/6/18
// Description: 音乐播放器Api

import 'package:devicelocale/devicelocale.dart';
import 'package:tone_snap/data/models/player_model.dart';
import 'package:tone_snap/global/app_config.dart';
import 'package:tone_snap/data/models/browse_model.dart';
import 'package:tone_snap/data/network/dio_client.dart';
import 'package:tone_snap/data/models/next_model.dart';
import 'package:tone_snap/utils/date_util.dart';

class MusicApi {
  static const String baseUrl = 'https://music.youtube.com/youtubei/v1/';

  /// 首页browse接口
  static Future<T?> browse<T>({
    String? visitorData,
    Map<String, dynamic>? queryParameters,
    T Function(Map<String, dynamic>)? formJson,
  }) async {
    String date = DateUtil.getSevenDaysAgo();
    String locale = await Devicelocale.currentLocale ?? AppConfig.defaultLocale;
    final body = {
      "context": {
        "client": {
          "visitorData": visitorData,
          "clientName": "WEB_REMIX",
          // "clientVersion": "1.$date.01.00",
          "clientVersion": "1.20240607.01.00",
          "platform": "DESKTOP",
          "hl": locale,
          // "gl": AppConfig.isoCode
          "gl": 'HK'
        }
      }
    };
    T? resultModel;
    await DioClient().request<T>(
      'browse',
      requestMethod: RequestMethod.post,
      data: body,
      queryParameters: queryParameters,
      formJson: formJson,
      success: (model) => resultModel = model,
    );
    return resultModel;
  }

  /// next接口
  static Future<NextModel?> next({String? visitorData, String? playlistId, String? videoId}) async {
    String date = DateUtil.getSevenDaysAgo();
    String locale = await Devicelocale.currentLocale ?? AppConfig.defaultLocale;
    final body = {
      "context": {
        "client": {
          "visitorData": visitorData,
          "clientName": "WEB_REMIX",
          "clientVersion": "1.$date",
          "platform": "DESKTOP",
          "hl": locale,
          // "gl": AppConfig.isoCode
          "gl": 'HK'
        }
      }
    };
    Map<String, dynamic>? queryParameters = {
      'prettyPrint': false,
      'playlistId': playlistId,
      'videoId': videoId
    };
    NextModel? nextModel;
    await DioClient().request<NextModel>(
      'next',
      showLoading: true,
      showToast: true,
      requestMethod: RequestMethod.post,
      data: body,
      queryParameters: queryParameters,
      formJson: NextModel.fromMap,
      success: (model) => nextModel = model,
    );
    return nextModel;
  }

  /// player接口
  static Future<PlayerModel?> player({String? visitorData, String? videoId}) async {
    String date = DateUtil.getSevenDaysAgo();
    final body = {
      "context": {
        "client": {
          "clientName": "ANDROID_MUSIC",
          "clientVersion": "6.07.1",
          "platform": "MOBILE",
          "browserVersion":"125.0.0.0"
        }
      }
    };
    Map<String, dynamic>? queryParameters = {
      'prettyPrint': false,
      'playlistId': null,
      'videoId': videoId
    };
    PlayerModel? playerModel;
    await DioClient().request<PlayerModel>(
      'player',
      showLoading: true,
      showToast: true,
      requestMethod: RequestMethod.post,
      data: body,
      queryParameters: queryParameters,
      formJson: PlayerModel.fromMap,
      success: (model) => playerModel = model,
    );
    return playerModel;
  }
}
