import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tone_snap/components/base_scrollbar.dart';
import 'package:tone_snap/components/view_state_widget.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/modules/sideb/widgets/music_appbar.dart';
import 'package:tone_snap/modules/sideb/widgets/music_item.dart';
import 'package:tone_snap/modules/sideb/widgets/remove_padding_music_bar.dart';

import 'offline_controller.dart';

class OfflineView extends StatelessWidget {
  OfflineView({super.key});

  final controller = Get.find<OfflineController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Stack(
        children: [
          _buildPageBg(),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const MusicAppbar(
                title: 'Offline Songs',
              ),
              _buildTotal(),
              _buildList(),
              const RemovePaddingMusicBar(),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildPageBg() {
    return Image.asset(
      Assets.sideBLoveSongsBg,
      width: 1.sw,
      height: 1.sh,
      fit: BoxFit.cover,
    );
  }

  Widget _buildTotal() {
    return Padding(
      padding: EdgeInsets.only(top: 34.h, bottom: 10.h, left: 18.w, right: 18.w),
      child: Obx(() {
        return Text(
          '${controller.offlineList.length} Songs',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18.sp,
            overflow: TextOverflow.ellipsis,
          ),
        );
      }),
    );
  }

  Widget _buildList() {
    return Expanded(
      child: Obx(() {
        return ViewStateWidget(
          viewState: controller.viewState.value,
          child: BaseScrollbar(
            child: Obx(() {
              return ListView.builder(
                itemCount: controller.offlineList.length,
                itemBuilder: (context, index) {
                  return MusicItem(
                    musicModel: controller.offlineList[index],
                    showDownload: false,
                    onTapItem: () => controller.onTapItem(controller.offlineList[index].value),
                  );
                },
              );
            }),
          ),
        );
      }),
    );
  }
}
