// Author: fengshengxiong
// Date: 2024/6/21
// Description: 自定义/收藏播放列表Item

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/network_image_widget.dart';
import 'package:tone_snap/data/models/playlist_model.dart';
import 'package:tone_snap/data/storage/playlists_box.dart';
import 'package:tone_snap/routes/app_routes.dart';
import 'package:tone_snap/utils/obj_util.dart';

class PlaylistItem extends StatelessWidget {
  const PlaylistItem({super.key, required this.playlistModel, required this.index, this.onTapItem});

  final int index;
  final PlaylistModel playlistModel;
  final Function()? onTapItem;

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTapItem ?? _onTapItem,
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 12.h, horizontal: 16.w),
          child: Row(
            children: [
              NetworkImageWidget(
                url: index == 0 ? PlaylistsBox().getFirstCoverUrl(playlistModel.id) : playlistModel.coverUrl,
                width: 60.w,
                height: 60.w,
                radius: 8.r,
              ),
              SizedBox(width: 14.w),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      ObjUtil.getStr(playlistModel.title),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.sp,
                      ),
                    ),
                    SizedBox(height: 4.h),
                    Text(
                      index == 0 ? '${playlistModel.musicList?.length} songs' : ObjUtil.getStr(playlistModel.subtitle),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: const Color(0xFF999999),
                        fontSize: 12.sp,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _onTapItem() {
    if (index == 0) {
      Get.toNamed(AppRoutes.customPlaylist, arguments: playlistModel.id);
    } else {
      Get.toNamed(AppRoutes.albumSongList, arguments: {
        'browseId': playlistModel.id,
        'params': playlistModel.params,
        'coverUrl': playlistModel.coverUrl,
        'title': playlistModel.title,
        'subtitle': playlistModel.subtitle,
      });
    }
  }
}
