import 'package:didvan/config/theme_data.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/views/customize_category/customize_category_state.dart';
import 'package:didvan/views/widgets/didvan/scaffold.dart';
import 'package:didvan/views/widgets/didvan/switch.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../config/design_config.dart';
import '../../constants/assets.dart';
import '../widgets/didvan/button.dart';
import '../widgets/shimmer_placeholder.dart';
import '../widgets/state_handlers/empty_state.dart';
import '../widgets/state_handlers/sliver_state_handler.dart';

class NotificationStatusStep extends StatefulWidget {
  final Map<String, dynamic> pageData;

  const NotificationStatusStep({super.key, required this.pageData});

  @override
  State<NotificationStatusStep> createState() => _NotificationStatusStepState();
}

class _NotificationStatusStepState extends State<NotificationStatusStep> {
  late CustomizeCategoryState state;

  @override
  void initState() {
    super.initState();
    state = context.read<CustomizeCategoryState>();
    if (widget.pageData['toTimer'] != null) {
      state.toTimer = widget.pageData['toTimer'];
    }

    Future.delayed(
      Duration.zero,
      () => state.getNotificationStatuses(),
    );
  }

  @override
  Widget build(BuildContext context) {
    state = context.watch<CustomizeCategoryState>();

    return Material(
      child: Stack(
        children: [
          DidvanScaffold(
            appBarData: AppBarData(
              hasBack: true,
              title: 'شخصی‌سازی برنامه',
            ),
            physics: const BouncingScrollPhysics(),
            hidePlayer: true,
            padding: const EdgeInsets.all(16),
            showSliversFirst: false,
            slivers: [
              Consumer<CustomizeCategoryState>(
                builder: (context, state, child) =>
                    SliverStateHandler<CustomizeCategoryState>(
                  onRetry: state.getNotificationStatuses,
                  state: state,
                  itemPadding: const EdgeInsets.only(bottom: 16),
                  childCount: state.notifs.length,
                  placeholder: ShimmerPlaceholder(
                    height: 48,
                    width: MediaQuery.sizeOf(context).width,
                    borderRadius: DesignConfig.mediumBorderRadius,
                  ),
                  centerEmptyState: false,
                  enableEmptyState: false,
                  emptyState: EmptyState(
                    asset: Assets.emptyChat,
                    title: 'اولین نظر را بنویسید...',
                  ),
                  builder: (context, state, index) => Column(
                    children: [
                      Center(
                        child: Column(
                          children: [
                            DidvanSwitch(
                                value: state.notifs[index].selected!,
                                title: state.notifs[index].name!,
                                onChanged: (val) {
                                  state.changeSelected(index, state.notifs,
                                      state.selectedNotifIds);
                                }),
                            index != state.notifs.length - 1
                                ? Padding(
                                    padding: const EdgeInsets.only(
                                        bottom: 16, top: 16),
                                    child: Divider(
                                      color:
                                          Theme.of(context).colorScheme.border,
                                      height: 1,
                                    ),
                                  )
                                : const SizedBox()
                          ],
                        ),
                      ),
                      SizedBox(
                        height: index == state.notifs.length - 1 ? 92 : 0,
                      )
                    ],
                  ),
                ),
              ),
            ],
            children: [
              DidvanText(
                "همیشه با اعلانات شخصی‌سازی شده خود بروز بمانید",
                style: Theme.of(context).textTheme.bodyMedium,
              )
            ],
          ),
          Positioned(
              bottom: 40,
              left: 16,
              right: 16,
              child: DidvanButton(
                onPressed: state.appState == AppState.idle
                    ? () async {
                        await state.putNotificationsStatuses(context);
                      }
                    : null,
                title: 'تایید',
                style: ButtonStyleMode.primary,
              ))
        ],
      ),
    );
  }
}
