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;
  late PlaylistModel playlistModel;
  var title = ''.obs;
  var showSearch = false.obs;
  var textEditingController = TextEditingController();
  var viewState = ViewState.normal.obs;
  var musicList = <MusicModel>[].obs;

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

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

  void getPlaylistMode() {
    if (PlaylistsBox().getPlaylistModel(playlistModelId) != null) {
      playlistModel = PlaylistsBox().getPlaylistModel(playlistModelId)!;
      title.value = playlistModel.title;
      _getList();
    }
  }

  void _getList() {
    musicList.value = playlistModel.musicList?.toList() ?? <MusicModel>[];
    viewState.value = musicList.isNotEmpty ? ViewState.normal : ViewState.empty;
  }

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

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

  Future<void> onTapPlayAll(bool isShuffle) async {
    if (musicList.isNotEmpty) {
      int index = 0;
      if (isShuffle && musicList.length > 1) {
        final n = musicList.indexWhere((e) => e.videoId == musicPlayerController.getMusicModel()?.videoId);
        if (n != -1) {
          index = NumUtil.getRandomNumberExcludingCurrent(0, musicList.length, n);
        } else {
          index = NumUtil.getRandomNumber(0, musicList.length);
        }
      }
      musicPlayerController.playMusic(musicList[index].videoId, playList: musicList);
    }
  }

  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 = <MusicModel>[];
      playlistModel.musicList!.map((e) {
        if (e.title!.toLowerCase().contains(keywords.trim().toLowerCase())) {
          result.add(e);
        }
      }).toList();
      musicList.value = result;
    } else {
      _getList();
    }
  }

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