import 'package:flutter/material.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
import 'package:rogapp/model/destination.dart';
import 'package:rogapp/pages/destination/destination_controller.dart';
import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/widgets/bottom_sheet_new.dart';
import 'package:rogapp/widgets/bottom_sheet_widget.dart';

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

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

  Image getImage(int index){
    if(indexController.locations[0].collection[index].properties!["photos"] == null || indexController.locations[0].collection[index].properties!["photos"] == ""){
      return Image(image: AssetImage('assets/images/empty_image.png'));
    }
    else{
      return Image(
        image: NetworkImage(indexController.locations[0].collection[index].properties!["photos"]),
        errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
          return Image.asset("assets/images/empty_image.png");
        },
        );
    }
  }

  void changeCurrentFeature(GeoJsonFeature fs){
    if(indexController.currentFeature.length > 0){
      indexController.currentFeature.clear();
    }
    indexController.currentFeature.add(fs);
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() =>
              indexController.locations.length > 0 ?
              ListView.builder(
              itemCount: indexController.locations[0].collection.length,
              shrinkWrap: true,
              itemBuilder: (_, index){
                bool _is_found = false;
                for(Destination d in destinationController.destinations){
                  if(indexController.locations[0].collection[index].properties!['location_id'] == d.location_id){
                    _is_found = true;
                    break;
                  }
                }
                return Card(
                  child: ListTile(
                    selected: _is_found,
                    
                    selectedTileColor: Colors.yellow.shade200,
                    onTap: (){
                      GeoJsonFeature gf = indexController.locations[0].collection[index];
                      changeCurrentFeature(gf);
                      showModalBottomSheet(
                          isScrollControlled: true,
                          context: context,
                          //builder: (context) => BottomSheetWidget(),
                          builder:((context) => BottomSheetNew())
                        );
                    },
                    leading: getImage(index),
                    title: indexController.locations[0].collection[index].properties!['location_name'] != null ? Text(indexController.locations[0].collection[index].properties!['location_name'].toString()) : Text(""),
                    subtitle: indexController.locations[0].collection[index].properties!['category'] != null ? Text(indexController.locations[0].collection[index].properties!['category']) : Text(""),
                    trailing: indexController.locations[0].collection[index].properties!['sub_loc_id'] != null ? Text(indexController.locations[0].collection[index].properties!['sub_loc_id']) : Text(""),
                  ),
                );
              },
            ) : Container(width: 0, height: 0,),
            );
  }
}