import 'package:app/widgets/room/file_drawer.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';
import '../viewmodel/stu_room_vm.dart';

class BottomBar extends StatefulWidget {
  final void Function()? onTap;

  const BottomBar({super.key, this.onTap});

  @override
  State<BottomBar> createState() => _BottomBarState();
}

class _BottomBarState extends State<BottomBar> {
  ///显示文件
  void _handShowFile() {
    final vm = context.read<StuRoomVM>();
    if (vm.selfInfo == null) return;
    showFileDialog(
      context,
      files: vm.selfInfo!.filesList,
      onConfirm: (file) {
        vm.uploadFile(file);
      },
    );
  }


  @override
  Widget build(BuildContext context) {
    final vm = context.watch<StuRoomVM>();

    if (vm.roomInfo.roomStatus != 1) {
      return SizedBox();
    }
    return Container(
      decoration: BoxDecoration(
        color: Color(0xff232426),
      ),
      height: 70,
      child: Consumer<StuRoomVM>(
        builder: (context, vm, _) {
          //摄像头开关
          return Row(
            children: [
              BarItem(
                title: "摄像头",
                icon: vm.cameraClose ? RemixIcons.video_off_fill : RemixIcons.video_on_fill,
                isOff: vm.cameraClose,
                onTap: () {
                  vm.changeCameraSwitch(value: vm.cameraClose);
                },
              ),
              BarItem(
                title: "麦克风",
                icon: vm.micClose ? RemixIcons.mic_off_fill : RemixIcons.mic_fill,
                isOff: vm.micClose,
                onTap: () {
                  vm.changeMicSwitch(value: vm.micClose);
                },
              ),
              BarItem(
                title: "声音",
                icon: vm.speakerClose
                    ? RemixIcons.volume_mute_fill
                    : RemixIcons.volume_up_fill,
                isOff: vm.speakerClose,
                onTap: () {
                  vm.changeSpeakerSwitch(value: vm.speakerClose);
                },
              ),
              BarItem(
                title: "举手",
                icon: RemixIcons.hand,
                onTap: () {
                  vm.changeHandSwitch();
                  widget.onTap?.call();
                },
              ),
              BarItem(
                title: "上传",
                icon: RemixIcons.upload_2_fill,
                onTap: _handShowFile,
              ),
            ],
          );
        },
      ),
    );
  }
}

class BarItem extends StatelessWidget {
  final String title;
  final IconData icon;
  final bool isOff;
  final void Function()? onTap;

  const BarItem({
    super.key,
    required this.title,
    required this.icon,
    this.isOff = false,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: InkWell(
        onTap: onTap,
        child: Container(
          color: isOff ? Colors.red : null,
          child: Column(
            spacing: 3,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(icon, color: Colors.white),
              Text(
                title,
                style: TextStyle(color: Colors.white70, fontSize: 12),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
