import 'package:background_downloader/background_downloader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/divider_widget.dart';
import 'package:tone_snap/components/get_bind_widget.dart';
import 'package:tone_snap/components/network_image_widget.dart';
import 'package:tone_snap/data/models/music_model.dart';
import 'package:tone_snap/data/storage/offline_box.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/res/themes/app_colors.dart';
import 'package:tone_snap/utils/obj_util.dart';

import 'more_bottom_sheet_controller.dart';

class MoreBottomSheetView extends StatelessWidget {
  MoreBottomSheetView({super.key, required this.musicModel, this.playlistModelId});

  final controller = Get.put(MoreBottomSheetController());
  final Rx<MusicModel> musicModel;
  final String? playlistModelId;

  @override
  Widget build(BuildContext context) {
    controller.setMusicModel(musicModel, playlistModelId: playlistModelId);
    return GetBindWidget(
      bind: controller,
      child: IntrinsicHeight(
        child: Container(
          width: 1.sw,
          padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom),
          decoration: const BoxDecoration(
            color: Color(0xFF282A2C),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(18), topRight: Radius.circular(18),
            ),
          ),
          child: Column(
            children: [
              _buildIndicator(),
              _buildMusicInfo(),
              _buildDownload(),
              _buildItem('Add to playlist', Assets.sideBAddToPlaylist, controller.onTapAddToPlaylist),
              // _buildItem('Add to queue', Assets.sideBAddToQueue, () {}),
              if (playlistModelId != null) ...[
                _buildItem('Remove from list', Assets.sideBMoreRemove, controller.onTapRemove),
              ],
              _buildItem('Report', Assets.sideBReport, controller.onTapReport),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildIndicator() {
    return Container(
      width: 28,
      height: 4,
      margin: const EdgeInsets.symmetric(vertical: 8).h,
      decoration: BoxDecoration(
        color: const Color(0xFF555555),
        borderRadius: BorderRadius.circular(20),
      ),
    );
  }

  Widget _buildMusicInfo() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 18.w),
      child: Column(
        children: [
          SizedBox(height: 20.h),
          Row(
            children: [
              Obx(() {
                return NetworkImageWidget(
                  url: musicModel.value.coverUrl,
                  width: 50.w,
                  height: 50.w,
                  radius: 10.r,
                );
              }),
              SizedBox(width: 12.w),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      ObjUtil.getStr(musicModel.value.title),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 14.sp,
                      ),
                    ),
                    Text(
                      ObjUtil.getStr(musicModel.value.subtitle),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: const Color(0xFF666666),
                        fontSize: 12.sp,
                      ),
                    ),
                  ],
                ),
              ),
              GestureDetector(
                onTap: controller.onTapLove,
                child: ClipOval(
                  child: Container(
                    width: 32.w,
                    height: 32.w,
                    color: const Color(0x26FFFFFF),
                    child: FittedBox(
                      fit: BoxFit.none,
                      child: Obx(() {
                        return Image.asset(
                          controller.isLove.value ? Assets.sideBLoveSolid : Assets.sideBLove,
                          width: 16.w,
                          height: 16.w,
                        );
                      }),
                    ),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 15.5.h),
          const DividerWidget(color: Color(0xFF444444)),
        ],
      ),
    );
  }

  Widget _buildDownload() {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: controller.onTapDownload,
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 18.w, vertical: 12.h),
          child: Row(
            children: [
              SizedBox(
                width: 24.w,
                height: 24.w,
                child: Obx(() {
                  if (OfflineBox().checkDownloaded(musicModel.value.videoId) || musicModel.value.taskStatus == TaskStatus.complete) {
                    return Image.asset(Assets.sideBDownloaded);
                  } else {
                    if (musicModel.value.taskStatus == TaskStatus.enqueued) {
                      return CircularProgressIndicator(
                        color: seedColor,
                        strokeWidth: 2.w,
                      );
                    } else if (musicModel.value.taskStatus == TaskStatus.running) {
                      return CircularProgressIndicator(
                        value: musicModel.value.progress,
                        backgroundColor: seedColor.withOpacity(0.2),
                        valueColor: const AlwaysStoppedAnimation<Color>(seedColor),
                        strokeWidth: 2.w,
                      );
                    } else {
                      return Image.asset(Assets.sideBNotDownload1);
                    }
                  }
                }),
              ),
              SizedBox(width: 12.w),
              Expanded(
                child: Obx(() {
                  var text = '';
                  if (OfflineBox().checkDownloaded(musicModel.value.videoId) || musicModel.value.taskStatus == TaskStatus.complete) {
                    text = ' Removed';
                  } else {
                    if (musicModel.value.taskStatus == TaskStatus.enqueued
                    || musicModel.value.taskStatus == TaskStatus.running) {
                      text = 'Cancel download';
                    } else {
                      text = 'Download';
                    }
                  }
                  return Text(
                    text,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 14.sp,
                    ),
                  );
                }),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildItem(String label, String img, Function() onTap) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 18.w, vertical: 12.h),
          child: Row(
            children: [
              Image.asset(
                img,
                width: 24.w,
                height: 24.w,
              ),
              SizedBox(width: 12.w),
              Expanded(
                child: Text(
                  label,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14.sp,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
