import 'package:get/get.dart';
import 'package:tone_snap/data/api/music_api.dart';
import 'package:tone_snap/data/models/browse_album_model.dart';
import 'package:tone_snap/data/models/music_model.dart';
import 'package:tone_snap/modules/sideb/controllers/music_player_controller.dart';
import 'package:tone_snap/routes/app_routes.dart';
import 'package:tone_snap/utils/obj_util.dart';

class AlbumController extends GetxController {
  var musicPlayerController = MusicPlayerController.to;
  String? browseId;
  String? params;

  /// 背景图、标题 、描述
  var bgThumbnail = Rx<String?>(null);
  var title = Rx<String?>(null);
  var description = Rx<String?>(null);

  /// 专辑列表
  var albumList = <MusicModel>[].obs;

  @override
  void onInit() {
    super.onInit();
    browseId = Get.arguments['browseId'];
    params = Get.arguments['params'];
  }

  @override
  void onReady() {
    super.onReady();
    _requestAlbum();
  }

  /// 请求专辑
  Future<void> _requestAlbum() async {
    Map<String, dynamic> queryParameters = {
      'browseId': browseId,
      'params': params,
      'prettyPrint': false
    };
    BrowseAlbumModel? browseAlbumModel = await MusicApi.browse<BrowseAlbumModel>(queryParameters: queryParameters, formJson: BrowseAlbumModel.fromMap);
    if (browseAlbumModel != null) {
      var thumbnails = browseAlbumModel.background?.musicThumbnailRenderer?.thumbnail?.thumbnails;
      if (thumbnails != null && thumbnails.isNotEmpty) {
        bgThumbnail.value = thumbnails.last.url;
      }
      var tabs = browseAlbumModel.contents?.twoColumnBrowseResultsRenderer?.tabs;
      if (tabs != null && tabs.isNotEmpty) {
        var contents = tabs[0].tabRenderer?.content?.sectionListRenderer?.contents;
        if (contents != null && contents.isNotEmpty) {
          var runs = contents[0].musicResponsiveHeaderRenderer?.title?.runs;
          if (runs != null && runs.isNotEmpty) {
            title.value = runs[0].text;
          }
          var runs1 = contents[0].musicResponsiveHeaderRenderer?.description?.musicDescriptionShelfRenderer?.description?.runs;
          if (runs1 != null && runs1.isNotEmpty) {
            description.value = runs1.map((e) => e.text).join();
          }
        }
      }

      var contents = browseAlbumModel.contents?.twoColumnBrowseResultsRenderer?.secondaryContents?.sectionListRenderer?.contents;
      if (contents != null && contents.isNotEmpty) {
        var contents1 = contents[0].musicShelfRenderer?.contents;
        if (contents1 != null && contents1.isNotEmpty) {
          for (var o in contents1) {
            var playListModel = MusicModel();
            var flexColumns = o.musicResponsiveListItemRenderer?.flexColumns;
            if (flexColumns != null && flexColumns.isNotEmpty) {
              var runs = flexColumns[0].musicResponsiveListItemFlexColumnRenderer?.text?.runs;
              if (runs != null && runs.isNotEmpty) {
                playListModel.title = runs[0].text;
              }
              for (var o in flexColumns) {
                var runs = o.musicResponsiveListItemFlexColumnRenderer?.text?.runs;
                if (runs != null && runs.isNotEmpty) {
                  playListModel.subTitle = runs.map((e) => e.text).join();
                }
              }
            }
            var fixedColumns = o.musicResponsiveListItemRenderer?.fixedColumns;
            if (fixedColumns != null && fixedColumns.isNotEmpty) {
              var runs = fixedColumns[0].musicResponsiveListItemFixedColumnRenderer?.text?.runs;
              if (runs != null && runs.isNotEmpty) {
                playListModel.subTitle = '${ObjUtil.getStr(playListModel.subTitle)} • ${runs.map((e) => e.text).join()}';
              }
            }
            var playlistItemData = o.musicResponsiveListItemRenderer?.playlistItemData;
            if (playlistItemData != null) {
              playListModel.videoId = playlistItemData.videoId;
            }
            albumList.add(playListModel);
          }
        }
      }
    }
  }

  /// 点击专辑列表歌曲
  void onTapAlbumItem(int index, MusicModel model) {
    Get.toNamed(AppRoutes.playPage, arguments: {'playList': albumList, 'videoId': model.videoId});
  }
}
