// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/utils/tools/tools.dart';
import 'package:qadirneyriz/widgets/card_meeting.dart';
import 'package:qadirneyriz/widgets/custom_appbar.dart';
import 'package:qadirneyriz/widgets/today_widget.dart';
import 'package:shamsi_date/shamsi_date.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:qadirneyriz/config/config.dart';
import 'package:qadirneyriz/screens/home/state.dart';
import 'package:qadirneyriz/utils/enums/status.dart';
import 'package:qadirneyriz/widgets/loading_widget.dart';

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
    HomeState homeState = Provider.of(context, listen: false);
    Future.delayed(Duration.zero, () async {
      await homeState.getTodayMeetings();
    });
  }

  @override
  Widget build(BuildContext context) {
    final Jalali shamsi = Jalali.now(); // دریافت تاریخ شمسی کنونی
    String formattedDate =
        '${shamsi.day} ${Tools.getMonthName(shamsi.month)} ${shamsi.year}'; // فرمت کردن تاریخ

    return Consumer<HomeState>(
      builder: (context, value, child) {
        switch (value.todayMettingsStatus) {
          case Status.ready:
            return CustomScrollView(
              slivers: [
                const CustomAppbar(),
                SliverToBoxAdapter(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 20, vertical: 10),
                    child: Container(
                      decoration: BoxDecoration(
                          color: const Color(0xffF4F9F6),
                          boxShadow: [
                            BoxShadow(
                                color: config.ui.mainGray.withOpacity(.1),
                                spreadRadius: .1,
                                offset: const Offset(0, 5),
                                blurRadius: 6)
                          ],
                          borderRadius: BorderRadius.circular(25)),
                      width: double.infinity,
                      child: Padding(
                        padding: const EdgeInsets.all(20),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Icon(
                              Icons.edit_outlined,
                              color: config.ui.mainGreen,
                            ),
                            const SizedBox(
                              width: 10,
                            ),
                            Expanded(
                              child: Text(
                                  style: const TextStyle(fontSize: 13),
                                  value.todayMeetingsModel!.note ?? ''),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
                SliverToBoxAdapter(
                  child: TodayWidget(formattedDate: formattedDate),
                ),
                SliverToBoxAdapter(
                  child: SizedBox(
                    height: 165,
                    child: value.todayMeetingsModel!.meetings!.isNotEmpty
                        ? ListView.builder(
                            scrollDirection: Axis.horizontal,
                            itemCount:
                                value.todayMeetingsModel!.meetings!.length,
                            itemBuilder: (BuildContext context, int index) {
                              final items =
                                  value.todayMeetingsModel!.meetings![index];
                              return CustomCardMeeting(
                                titel: items.subject!.subject ?? '',
                                fromTime: items.azHour ?? '',
                                toTime: items.taHour ?? "",
                                location: items.location!.address ?? '',
                                date: items.dateJalali ?? '',
                                cardId: items.id ?? 0,
                              );
                            },
                          )
                        : Center(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Icon(Icons.error_outline,
                                    size: 40,
                                    color: config.ui.mainGray.withOpacity(.5)),
                                const SizedBox(
                                  height: 20,
                                ),
                                Text(
                                  AppLocalizations.of(context)!
                                      .nomeetingfortoday,
                                  style: TextStyle(
                                      color:
                                          config.ui.mainGray.withOpacity(.5)),
                                ),
                              ],
                            ),
                          ),
                  ),
                ),
                SliverPadding(
                  padding:
                      const EdgeInsets.symmetric(vertical: 30, horizontal: 10),
                  sliver: SliverToBoxAdapter(
                    child: StaggeredGrid.count(
                      crossAxisCount: 4,
                      mainAxisSpacing: 4,
                      crossAxisSpacing: 4,
                      children: [
                        StaggeredGridTile.count(
                          crossAxisCellCount: 2,
                          mainAxisCellCount: 1,
                          child: ItemInGrid(
                            icon: Icons.assessment,
                            backColor: const Color(0xff03C85F),
                            text: AppLocalizations.of(context)!.reports,
                            onTap: () {},
                          ),
                        ),
                        StaggeredGridTile.count(
                          crossAxisCellCount: 2,
                          mainAxisCellCount: 2,
                          child: ItemInGrid(
                            icon: Icons.people,
                            backColor: const Color(0xff04A54F),
                            text: AppLocalizations.of(context)!.meetings,
                            onTap: () {
                              context.pushNamed('navigate',
                                  pathParameters: {'tab': '1'});
                            },
                          ),
                        ),
                        StaggeredGridTile.count(
                          crossAxisCellCount: 2,
                          mainAxisCellCount: 2,
                          child: ItemInGrid(
                            icon: Icons.calendar_today,
                            backColor: const Color(0xff37A068),
                            text: AppLocalizations.of(context)!.events,
                            onTap: () {},
                          ),
                        ),
                        StaggeredGridTile.count(
                          crossAxisCellCount: 2,
                          mainAxisCellCount: 1,
                          child: ItemInGrid(
                            icon: Icons.exit_to_app,
                            backColor: const Color(0xff00843D),
                            text: AppLocalizations.of(context)!.exit,
                            onTap: () {},
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            );
          case Status.loading:
            return const LoadingWidget();
          default:
            return Container();
        }
      },
    );
  }
}

class ItemInGrid extends StatelessWidget {
  final IconData icon;
  final Color backColor;
  final String text;
  final void Function() onTap;
  const ItemInGrid(
      {super.key,
      required this.icon,
      required this.backColor,
      required this.text,
      required this.onTap});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(4.0),
      child: GestureDetector(
        onTap: onTap,
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20),
            color: backColor,
            boxShadow: [
              BoxShadow(
                  color: config.ui.mainGray.withOpacity(.5),
                  spreadRadius: .1,
                  offset: const Offset(0, 2),
                  blurRadius: 6)
            ],
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                icon,
                size: 40,
                color: Colors.white,
              ),
              const SizedBox(
                height: 8,
              ),
              Text(
                text,
                style: const TextStyle(
                  color: Colors.white,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
