
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:rogapp/pages/destination/destination_controller.dart';
import 'package:rogapp/pages/destination_map/destination_map_page.dart';
import 'package:rogapp/pages/drawer/drawer_page.dart';
import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/widgets/destination_widget.dart';

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

  final DestinationController destinationController = Get.find<DestinationController>();

  final IndexController indexController = Get.find<IndexController>();

  final List<int> _items = List<int>.generate(50, (int index) => index);

  Future<void> showCurrentPosition() async {
    LocationPermission permission = await Geolocator.checkPermission();
    if (permission != LocationPermission.whileInUse ||
        permission != LocationPermission.always) {
      permission = await Geolocator.requestPermission();
    }
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high, forceAndroidLocationManager: true);
    indexController.rogMapController.move(LatLng(position.latitude, position.longitude), 14);
  }

  Image getImage(int index){
    if(destinationController.destinations[index].photos == null || destinationController.destinations[index].photos == ""){
      return const Image(image: AssetImage('assets/images/empty_image.png'));
    }
    else{
      return Image(image: NetworkImage(destinationController.destinations[index].photos!));
    }
  }

  Widget getRoutingImage(int route){
    switch (route) {
      case 0:
        return const Image(image: AssetImage('assets/images/p4_9_man.png'), width: 35.0,);
      case 1:
        return const Image(image: AssetImage('assets/images/p4_8_car.png'), width: 35.0,);
      case 2:
        return const Image(image: AssetImage('assets/images/p4_10_train.png'), width: 35.0,);
      default:
        return const Image(image: AssetImage('assets/images/p4_9_man.png'), width: 35.0,);
    }
  }

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        indexController.switchPage(AppPages.INITIAL);
        return false;
      },
      child: Scaffold(
        drawer: DrawerPage(),
        bottomNavigationBar: BottomAppBar(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(left:13.0),
                child: InkWell(
                  child: Obx((() => getRoutingImage(destinationController.travelMode.value))),
                  onTap: (){
                    Get.bottomSheet(
                        Obx(() => 
                          ListView(
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(top:30.0, bottom: 30),
                                child: Center(child: Text("select_travel_mode".tr, style: const TextStyle(fontSize: 22.0, color:Colors.red, fontWeight:FontWeight.bold),),),
                              ),
                              ListTile(
                                selected: destinationController.travelMode == 0 ? true : false,
                                selectedTileColor: Colors.amber.shade200,
                                leading: const Image(image: AssetImage('assets/images/p4_9_man.png'),),
                                title: Text("walking".tr),
                                onTap:(){
                                  destinationController.travelMode.value = 0;
                                  destinationController.PopulateDestinations();
                                  Get.back();
                                },
                                ),
                              ListTile(
                                selected: destinationController.travelMode == 1 ? true : false,
                                selectedTileColor: Colors.amber.shade200,
                                leading: const Image(image: AssetImage('assets/images/p4_8_car.png'),),
                                title: Text("driving".tr),
                                onTap:(){
                                  destinationController.travelMode.value = 1;
                                  destinationController.PopulateDestinations();
                                  Get.back();
                                },
                                ),
                              // ListTile(
                              //   selected: destinationController.travelMode == 2 ? true : false,
                              //   selectedTileColor: Colors.amber.shade200,
                              //   leading: Image(image: AssetImage('assets/images/p4_10_train.png'),),
                              //   title: Text("transit".tr),
                              //   onTap:(){
                              //     destinationController.travelMode.value = 2;
                              //     destinationController.PopulateDestinations();
                              //     Get.back();
                              //   },
                              //   ),
                            ],
                            
                          ),
                        ),
                        isScrollControlled:false,
                        backgroundColor: Colors.white,
                      ); 
                      //destinationController.PopulateDestinations();
                    }
                  ),
              )
              ,
                IconButton(
                  icon: const Icon(Icons.travel_explore, size: 35,), 
                    onPressed: (){
                      indexController.switchPage(AppPages.INITIAL);
                    }
                ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
            onPressed: (){
              //print("######");
              indexController.toggleDestinationMode();
            },
            tooltip: 'Increment',
            child: Obx(() => 
              indexController.desination_mode == 1 ?
              const Image(image: AssetImage('assets/images/list2.png'))
              :
              const Image(image: AssetImage('assets/images/map.png'))
            ),
            elevation: 4.0,
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        appBar:AppBar(
          automaticallyImplyLeading: true,
          title: Text("app_title".tr),
          actions: [
            InkWell(
              onTap: (){
                Get.toNamed(AppPages.CAMERA_PAGE);
              },
              child: destinationController.is_in_rog == true ?
                Image.asset("assets/images/basic-walking.gif",height: 10.0,)
              :
                destinationController.is_at_goal == true ?
                  IconButton(
                    onPressed:(){Get.toNamed(AppPages.CAMERA_PAGE);}, 
                    icon: const Icon(Icons.assistant_photo),
                  )
                :
                  IconButton(
                    onPressed:(){Get.toNamed(AppPages.CAMERA_PAGE);}, 
                    icon: const Icon(Icons.accessibility),
                  ),
            ),
            // Obx(() => 
            //   Text(indexController.connectionStatusName.value)
            // ),
            Obx(() => 
             ToggleButtons(
                  disabledColor: Colors.grey.shade200,
                  selectedColor: Colors.red,
                children: const <Widget>[
                  Icon(Icons.explore, size: 35.0,
                )],
                onPressed: (int index) {
                  destinationController.is_gps_selected.value = !destinationController.is_gps_selected.value;
                  if(destinationController.is_gps_selected.value){
                    destinationController.chekcs = 0;
                    destinationController.skip_gps = false;
                    //destinationController.resetRogaining();
                  }
                },
                isSelected: [destinationController.is_gps_selected.value],
              ),
            ),
            // IconButton(onPressed: (){
            //   showCurrentPosition();
            // },
            // icon: Icon(Icons.location_on_outlined))
          ],
        ),
        body: Obx(() => 
          indexController.desination_mode.value == 0 ?
                  DestinationWidget():
                  DestinationMapPage()
        )
      ),
    );
    
  }
}
