import 'package:flutter/material.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';

import '../Notifiers/CheckInProvider.dart';

class CheckInOutScreen extends StatefulWidget {
  final int getAttendanceStatus;
  const CheckInOutScreen({super.key,required this.getAttendanceStatus});

  @override
  State<CheckInOutScreen> createState() => _CheckInOutScreenState();
}

class _CheckInOutScreenState extends State<CheckInOutScreen> {

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var prov = Provider.of<CheckInOutProvider>(context, listen: false);
      prov.getLocationPermission(context);
      prov.getCurrentLocation();
    });
  }


  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    double screenWidth = MediaQuery.of(context).size.width;

    return Consumer<CheckInOutProvider>(
      builder: (context, provider, child) {
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar(context, widget.getAttendanceStatus==0? "Check In":"Check Out"),
            body:SafeArea(
              child: Container(
                child: SafeArea(
                  child: Column(
                    children: [
                      Expanded(
                        child: Stack(
                          children: [
                            GoogleMap(
                              myLocationEnabled: true,
                              zoomGesturesEnabled: true,
                              scrollGesturesEnabled: false,
                              initialCameraPosition: CameraPosition(
                                target: provider.currentLocationLatLng ??
                                    provider.startLocation,
                                zoom: 20.0,
                              ),
                              markers: provider.markers.toSet(),
                              myLocationButtonEnabled: true,
                              mapType: MapType.normal,
                              onMapCreated: (controller) {
                                provider.mapController = controller;
                                provider.getCurrentLocation();
                                provider.getLocationPermission(context);
                              },
                              onCameraMove: (position) {
                                provider.onCameraMove(position,context);
                              },
                            ),
                            Positioned(
                              left: 0,
                              right: 0,
                              bottom: 0,
                              child: Container(
                                height: size.height * 0.3,
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(30.0),
                                    topRight: Radius.circular(30.0),
                                  ),
                                ),
                                child: Column(
                                  crossAxisAlignment:
                                  CrossAxisAlignment.start,
                                  children: [
                                    SizedBox(height: 25),

                                    Padding(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 20.0),
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            "Share Your Details",
                                            style: TextStyle(
                                              color: AppColors.app_blue,
                                              fontFamily: "JakartaSemiBold"
                                            ),
                                          ),
                                          SizedBox(height: 10,),
                                          Text(
                                            "Location",
                                            style: TextStyle(
                                              fontFamily: "JakartaMedium",
                                              color: AppColors.semi_black,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    SizedBox(height: 5),
                                    Padding(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 20.0),
                                      child: Container(
                                        height: 50,

                                        alignment: Alignment.center,
                                        decoration: BoxDecoration(
                                          color: AppColors.text_field_color,
                                          borderRadius:
                                          BorderRadius.circular(14),
                                        ),
                                        child: Padding(
                                          padding:
                                          const EdgeInsets.fromLTRB(
                                              10.0, 0.0, 10, 0),
                                          child: TextFormField(
                                            controller:
                                            provider.locationController,
                                            keyboardType:
                                            TextInputType.text,
                                            decoration: InputDecoration(

                                              hintText:
                                              "Enter Check ${widget.getAttendanceStatus==0?"In":"Out"} Location",
                                              hintStyle: TextStyle(
                                                  fontWeight: FontWeight.w400,
                                                  fontSize: 14,
                                                color: Color(0xFF818181)
                                              ),
                                              enabledBorder:
                                              InputBorder.none,
                                              focusedBorder:
                                              InputBorder.none,
                                            ),

                                          ),
                                        ),
                                      ),
                                    ),
                                    if (provider.validateLocation != null)
                                      Padding(
                                        padding:
                                        const EdgeInsets.symmetric(
                                            horizontal: 20.0),
                                        child: Container(
                                          alignment: Alignment.topLeft,
                                          margin: EdgeInsets.only(
                                              top: 2.5,
                                              bottom: 2.5,
                                              left: 25),
                                          child: Text(
                                            provider.validateLocation!,
                                            textAlign: TextAlign.start,
                                            style: TextStyle(
                                              color: Colors.red,
                                            ),
                                          ),
                                        ),
                                      )
                                    else
                                      SizedBox(height: 5.0),
                                    SizedBox(height: 20),
                                    Padding(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 20.0),
                                      child: InkWell(
                                        onTap: () async {
                                          if (provider
                                              .locationController
                                              .text
                                              .isEmpty) {
                                            provider.validateLocation =
                                            "Please Enter location";
                                            provider.notifyListeners();
                                          } else {
                                            provider.validateLocation = "";

                                              provider.imgFromCamera(
                                                  context,widget.getAttendanceStatus);

                                          }
                                        },
                                        child: Container(
                                          alignment: Alignment.center,
                                          height: 45,
                                          width: screenWidth,

                                          decoration: BoxDecoration(
                                            color: AppColors.app_blue,
                                            borderRadius:
                                            BorderRadius.circular(15.0),
                                          ),
                                          child: Text(
                                           widget.getAttendanceStatus==0? "Check In":"Check Out",
                                            style: TextStyle(
                                              fontFamily: "JakartaMedium",
                                              fontSize: 16,
                                              color: Colors.white
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}


