import 'package:app/router/route_paths.dart';
import 'package:app/utils/permission.dart';
import 'package:app/utils/time.dart';
import 'package:app/widgets/base/button/index.dart';
import 'package:app/widgets/base/card/g_card.dart';
import 'package:app/widgets/base/config/config.dart';
import 'package:app/widgets/base/empty/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:go_router/go_router.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:remixicon/remixicon.dart';
import 'package:skeletonizer/skeletonizer.dart';

import '../viewmodel/home_view_model.dart';

class TodayCard extends StatefulWidget {
  const TodayCard({super.key});

  @override
  State<TodayCard> createState() => _TodayCardState();
}

class _TodayCardState extends State<TodayCard> {
  ///前往会议室
  void _goToRoom() {
    checkPermission(
      permissions: [Permission.microphone, Permission.camera],
      onGranted: () {
        final vm = context.read<HomeViewModel>();
        context.push(RoutePaths.tRoom, extra: vm.roomInfo);
      },
      onDenied: () {
        EasyLoading.showError("请开启权限");
      },
      onPermanentlyDenied: () {
        EasyLoading.showError("请手动开启麦克风和摄像头权限");
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<HomeViewModel>(
      builder: (context, vm, _) {
        return GCard(
          child: Visibility(
            visible: !vm.loading && vm.roomInfo == null,
            replacement: Skeletonizer(
              enabled: vm.loading,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        spacing: 10,
                        children: [
                          Skeleton.replace(
                            replacement: Bone.text(),
                            child: Text(vm.roomInfo?.roomName ?? ""),
                          ),
                        ],
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 5),
                        child: Text(
                          "和学生们一起专注学习、共同进步",
                          style: Theme.of(context).textTheme.labelMedium,
                        ),
                      ),
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 30),
                    child: Row(
                      spacing: 15,
                      children: [
                        _item(
                          title: "开始时间",
                          value: vm.roomInfo?.startTime ?? "",
                          icon: RemixIcons.time_line,
                          color: Color(0xff2b7efd),
                        ),
                        _item(
                          title: "结束时间",
                          value: vm.roomInfo?.endTime ?? "",
                          icon: RemixIcons.group_line,
                          color: Color(0xff00c74f),
                        ),
                        _item(
                          title: "时长",
                          value: "${formatSeconds(vm.roomMinutes * 60, 'hh小时mm分钟')} ",
                          icon: RemixIcons.book_open_line,
                          color: Color(0xffac45fd),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 30),
                    height: 45,
                    child: Button(
                      text: vm.canEnterRoom ? "开始自习室" : "未到开始时间",
                      type: ThemeType.success,
                      disabled: !vm.canEnterRoom,
                      onPressed: _goToRoom,
                    ),
                  ),
                ],
              ),
            ),
            child: SizedBox(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Empty(text: "未分配自习室"),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _item({
    required String title,
    required String value,
    required IconData icon,
    required Color color,
  }) {
    return Expanded(
      child: Container(
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: color.withValues(alpha: 0.2),
          borderRadius: BorderRadius.circular(10),
        ),
        child: Row(
          spacing: 10,
          children: [
            Container(
              width: 45,
              height: 45,
              decoration: BoxDecoration(
                color: color,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Icon(
                icon,
                color: Colors.white,
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title, style: Theme.of(context).textTheme.labelLarge),
                Text(value),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
