// ignore_for_file: deprecated_member_use

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/notification_time/notification_time_state.dart';
import 'package:didvan/views/notification_time/widgets/custom_cupertino_date_picker.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 '../widgets/didvan/button.dart';

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

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

  @override
  State<NotificationTime> createState() => _NotificationTimeState();
}

class _NotificationTimeState extends State<NotificationTime> {
  late NotificationTimeState state;

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

    if (widget.pageData['fromFav'] != null) {
      state.fromFav = widget.pageData['fromFav'];
    }

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

  @override
  Widget build(BuildContext context) {
    state = context.watch<NotificationTimeState>();
    return Material(
      child: Stack(
        children: [
          DidvanScaffold(
            appBarData: state.fromFav
                ? null
                : AppBarData(
                    hasBack: true,
                    title: 'زمان دریافت اعلان',
                  ),
            physics: const BouncingScrollPhysics(),
            hidePlayer: true,
            padding: const EdgeInsets.all(16),
            showSliversFirst: false,
            backgroundColor: Theme.of(context).colorScheme.background,
            slivers: const [],
            children: [
              Consumer<NotificationTimeState>(
                builder: (context, state, child) => state.appState ==
                        AppState.idle
                    ? Column(
                        children: [
                          DidvanText(
                            "لطفا زمان دریافت اعلانات خود را مشخص کنید",
                            style: Theme.of(context).textTheme.bodyMedium,
                          ),
                          Container(
                            margin: const EdgeInsets.all(24),
                            height: 210,
                            child: CustomCupertinoDatePicker(
                                disable: state.isAnytime,
                                itemExtent: 64,
                                selectedTime: state.selectedTime,
                                selectedStyle: Theme.of(context)
                                    .textTheme
                                    .titleMedium!
                                    .copyWith(
                                        color: state.isAnytime
                                            ? const Color(0xFFC8E0F4)
                                            : Theme.of(context)
                                                .colorScheme
                                                .white),
                                unselectedStyle:
                                    Theme.of(context).textTheme.titleSmall,
                                disabledStyle: Theme.of(context)
                                    .textTheme
                                    .titleMedium!
                                    .copyWith(
                                        color: Theme.of(context)
                                            .colorScheme
                                            .disabledText),
                                onSelectedItemChanged: (date) {
                                  state.selectedTime = date;
                                }),
                          ),
                          Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 16),
                            child: Container(
                              padding: const EdgeInsets.all(16),
                              decoration: BoxDecoration(
                                  color: Theme.of(context).colorScheme.surface,
                                  border: Border.all(
                                      color:
                                          Theme.of(context).colorScheme.border,
                                      width: 1),
                                  borderRadius: BorderRadius.circular(18)),
                              child: DidvanSwitch(
                                  value: state.isAnytime,
                                  title: "دریافت آنی اعلانات",
                                  onChanged: (val) {
                                    state.isAnytime = val;
                                    state.update();
                                  }),
                            ),
                          ),
                        ],
                      )
                    : const SizedBox(),
              )
            ],
          ),
          Positioned(
              bottom: 40,
              left: 16,
              right: 16,
              child: DidvanButton(
                onPressed: () {
                  state.putTime(context);
                },
                title: 'تایید',
                style: ButtonStyleMode.primary,
              ))
        ],
      ),
    );
  }
}
