
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/service_requests_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/models/timer_model.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/date_and_time/date_picker.dart';
import 'package:test_sa/views/widgets/date_and_time/time_picker.dart';
import 'package:test_sa/views/widgets/timer/app_timer.dart';

class TimeDurationView extends StatelessWidget {
  const TimeDurationView({Key ?key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //TODO user the same form key everywhere....
    final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
    return  Consumer<ServiceRequestsProvider>(
        builder: (context, serviceRequestProvider,child) {
          serviceRequestProvider.currentSelectedRequest?.visitDate = '';
          return Form(
            key: _formKey,
            child: SingleChildScrollView(
              child: Column(
                children: [
                  ADatePicker(
                    label: context.translation.visitDate,
                    hideShadow: true,
                    withIcon: false,
                    hint: context.translation.select,
                    height: 70.toScreenHeight,
                    date: DateTime.tryParse(serviceRequestProvider.currentSelectedRequest?.visitDate ?? ""),
                    formatDateWithTime: true,
                    onDatePicker: (selectedDate) {
                      if (selectedDate != null) {
                        showTimePicker(
                          context: context,
                          initialTime: TimeOfDay.now(),
                        ).then((selectedTime) {
                          // Handle the selected date and time here.
                          if (selectedTime != null) {
                            DateTime ? selectedDateTime = DateTime(
                              selectedDate.year,
                              selectedDate.month,
                              selectedDate.day,
                              selectedTime.hour,
                              selectedTime.minute,
                            );
                              serviceRequestProvider.currentSelectedRequest?.visitDate = selectedDateTime?.toIso8601String();
                          }
                        });
                      }
                    },
                  ),
                  8.height,
                  ATimePicker(
                    label: context.translation.startTime,
                    hint: context.translation.select,
                    withIcon: false,
                    withBorder: false,
                    height: 70.toScreenHeight,
                    time: serviceRequestProvider.selectedTime,
                    onTimePicker: (selectedTime) {
                      if (selectedTime != null) {
                        print('time selected $selectedTime');
                        serviceRequestProvider.selectedTime = selectedTime;
                      }
                    },
                  ),
                  8.height,
                  ATimePicker(
                    label: context.translation.endTime,
                    hint: context.translation.select,
                    withIcon: false,
                    withBorder: false,
                    height: 70.toScreenHeight,
                    time: serviceRequestProvider.selectedTime,
                    onTimePicker: (selectedTime) {
                      if (selectedTime != null) {
                        print('time selected $selectedTime');
                        serviceRequestProvider.selectedTime = selectedTime;
                      }
                    },
                  ),
                  8.height,
                  SizedBox(
                    width: double.infinity,
                    height: 70.toScreenHeight,
                    child: AppTimer(
                      label: context.translation.workingHours,
                      timer: TimerModel(),
                      decoration: BoxDecoration(
                        color: AppColor.background(context),
                        borderRadius: BorderRadius.circular(10),
                        // boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)],
                      ),
                      // enabled: serviceRequestProvider.currentSelectedRequest.date == null,
                      enabled: true,
                      onChange: (timer) async {
                        print('timer i got is ${timer.toString()}');
                        return true;
                      },
                    ),

                  ),
                  16.height,
                ],
              ),
            ),
          );
        }
    );
  }
}
