import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/view_state_widget.dart';
import 'package:tone_snap/data/models/music_model.dart';
import 'package:tone_snap/data/models/playlist_model.dart';
import 'package:tone_snap/data/storage/playlists_box.dart';
import 'package:tone_snap/modules/sideb/controllers/music_player_controller.dart';
import 'package:tone_snap/modules/sideb/more_playlist_bottom_sheet/more_playlist_bottom_sheet_view.dart';
import 'package:tone_snap/routes/app_routes.dart';
import 'package:tone_snap/utils/num_util.dart';
import 'package:tone_snap/utils/obj_util.dart';

class CustomPlaylistController extends GetxController {
  static CustomPlaylistController get to => Get.find<CustomPlaylistController>();
  var musicPlayerController = MusicPlayerController.to;
  late String playlistModelId;
  var playlistModel = Rx<PlaylistModel?>(null);
  var viewState = ViewState.normal.obs;
  var musicList = <Rx<MusicModel>>[].obs;
  var showSearch = false.obs;
  var textEditingController = TextEditingController();

  @override
  void onInit() {
    super.onInit();
    playlistModelId = Get.arguments;
    getPlaylistMode();
  }

  @override
  void onClose() {
    textEditingController.dispose();
    super.onClose();
  }

  void getPlaylistMode() {
    playlistModel.value = PlaylistsBox().getPlaylistModel(playlistModelId);
    playlistModel.update((fn) {});
    _getList();
  }

  void _getList() {
    if (playlistModel.value != null && playlistModel.value!.musicList != null) {
      musicList.value = playlistModel.value!.musicList!.map((e) => e.obs).toList();
    }
    viewState.value = musicList.isNotEmpty ? ViewState.normal : ViewState.empty;
  }

  String? getFirstCoverUrl() {
    if (musicList.isNotEmpty) {
      return musicList.first.value.coverUrl;
    }
    return null;
  }

  void onTapMore() {
    Get.bottomSheet(
      MorePlaylistBottomSheetView(
        playlistModel: playlistModel.value!,
      ),
    );
  }

  Future<void> onTapPlayAll(int type) async {
    if (musicList.isNotEmpty) {
      int index = 0;
      if (type == 1) {
        if (musicPlayerController.getMusicModel() != null) {
          int currentIndex = musicList.indexWhere((e) => e.value.videoId == musicPlayerController.getMusicModel()!.value.videoId);
          index = NumUtil.getRandomNumberExcludingCurrent(0, musicList.length, currentIndex);
        }
      }
      musicPlayerController.playMusic(musicList[index].value.videoId, playList: musicList.map((e) => e.value).toList());
    }
  }

  void onTapShowSearch(bool show) {
    if (show && musicList.isEmpty) {
      return;
    }
    showSearch.value = show;
    if (!showSearch.value) {
      textEditingController.clear();
      _getList();
    }
  }

  void onTapSearch(String keywords) {
    if (ObjUtil.isNotEmpty(keywords)) {
      final result = <Rx<MusicModel>>[].obs;
      playlistModel.value!.musicList!.map((e) {
        if (e.title!.toLowerCase().contains(keywords.trim().toLowerCase())) {
          result.add(e.obs);
        }
      }).toList();
      musicList.value = result;
    } else {
      _getList();
    }
  }

  void onTapItem(MusicModel musicModel) {
    Get.toNamed(AppRoutes.playPage, arguments: {
      'videoId': musicModel.videoId,
      'playList': musicList.map((e) => e.value).toList(),
    });
  }
}
