import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:generp/Utils/SharedpreferencesService.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:generp/services/api_calling.dart';
import 'package:geolocator/geolocator.dart';
import 'package:geolocator_platform_interface/src/enums/location_accuracy.dart' as geo_location;
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'package:location/location.dart' as Location;
import 'package:provider/provider.dart';

import '../Notifiers/CheckInProvider.dart';
import '../Utils/background_service.dart';
import '../Utils/commonServices.dart';
import 'FrontCameraCapture.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 Scaffold(
          appBar: appbar(context, widget.getAttendanceStatus==0? "Check In":"Check Out"),
          body:Container(
            child: SafeArea(
              child: Column(
                children: [
                  Expanded(
                    child: ClipRRect(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(30.0),
                        topRight: Radius.circular(30.0),
                      ),
                      child: Stack(
                        children: [
                          GoogleMap(
                            myLocationEnabled: true,
                            zoomGesturesEnabled: true,
                            initialCameraPosition: CameraPosition(
                              target: provider.currentLocationLatLng ??
                                  provider.startLocation,
                              zoom: 20.0,
                            ),
                            markers: provider.markers.toSet(),
                            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: Text(
                                      "Location",
                                      style: TextStyle(
                                        fontWeight: FontWeight.w400,
                                        color: Colors.grey,
                                      ),
                                    ),
                                  ),
                                  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(25),
                                      ),
                                      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
                                            ),
                                            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(30.0),
                                        ),
                                        child: Text(
                                         widget.getAttendanceStatus==0? "Check In":"Check Out",
                                          style: TextStyle(
                                            fontWeight: FontWeight.w500,
                                            color: Colors.white
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}


