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

import 'package:devicelocale/devicelocale.dart';
import 'package:tone_snap/data/app_config.dart';
import 'package:tone_snap/data/sideb/models/browse_model.dart';
import 'package:tone_snap/data/network/dio_client.dart';
import 'package:tone_snap/data/sideb/models/next_model.dart';
import 'package:tone_snap/data/sideb/models/player_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<BrowseModel?> browse({String? visitorData, Map<String, dynamic>? queryParameters}) 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
        }
      }
    };
    BrowseModel? browseModel;
    await DioClient().request<BrowseModel>(
      'browse',
      requestMethod: RequestMethod.post,
      data: body,
      queryParameters: queryParameters,
      formJson: BrowseModel.fromMap,
      success: (model) => browseModel = model,
    );
    return browseModel;
  }

  /// 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
        }
      }
    };
    Map<String, dynamic>? queryParameters = {
      'prettyPrint': false,
      'playlistId': playlistId,
      'videoId': videoId
    };
    NextModel? nextModel;
    await DioClient().request<NextModel>(
      'next',
      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',
      requestMethod: RequestMethod.post,
      data: body,
      queryParameters: queryParameters,
      formJson: PlayerModel.fromMap,
      success: (model) => playerModel = model,
    );
    return playerModel;
  }
}
