import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:geojson_vi/geojson_vi.dart';
import 'package:get/get.dart';
import 'package:gifunavi/model/destination.dart';
import 'package:gifunavi/pages/destination/destination_controller.dart';
import 'package:gifunavi/pages/index/index_controller.dart';
import 'package:gifunavi/pages/search/search_controller.dart';
//import 'package:gifunavi/widgets/bottom_sheets/bottom_sheet_start.dart';
//import 'package:gifunavi/widgets/bottom_sheets/bottom_sheet_goal.dart';
//import 'package:gifunavi/widgets/bottom_sheets/bottom_sheet_normal_point.dart';
import 'package:gifunavi/widgets/bottom_sheet_new.dart';

class SearchPage extends StatelessWidget {
  SearchPage({super.key});

  SearchBarController searchController = Get.find<SearchBarController>();
  IndexController indexController = Get.find<IndexController>();

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.white,
        leading: IconButton(
            onPressed: () {
              Get.back();
            },
            icon: const Icon(
              Icons.arrow_back_ios_new,
              color: Colors.black,
            )),
        centerTitle: true,
        //title: const CupertinoSearchTextField(),
      ),
      body: SingleChildScrollView(
        child: TypeAheadField<GeoJSONFeature>(
          // textFieldConfiguration: TextFieldConfiguration(
          //   autofocus: true,
          //   style: DefaultTextStyle.of(context).style.copyWith(
          //         fontStyle: FontStyle.normal,
          //         fontSize: 15.0,
          //       ),
          //   decoration: InputDecoration(
          //     border: const OutlineInputBorder(),
          //     hintText: "検索",
          //     prefixIcon: const Icon(Icons.search),
          //     suffixIcon: IconButton(
          //       icon: const Icon(Icons.clear),
          //       onPressed: () {
          //         // clear the text field
          //       },
          //     ),
          //   ),
          // ),
          onSelected: (GeoJSONFeature suggestion) {
            indexController.currentFeature.clear();
            indexController.currentFeature.add(suggestion);
            DestinationController destinationController =
                Get.find<DestinationController>();
            Destination des =
                destinationController.festuretoDestination(suggestion);
            Get.back();

            Widget bottomSheet = BottomSheetNew(destination: des);
            /*
            if (des.cp == -1 || des.cp == 0) {
              bottomSheet = BottomSheetStart(destination: des);
            } else if (des.cp == -2 || des.cp == 0) {
              bottomSheet = BottomSheetGoal(destination: des);
            } else {
              bottomSheet = BottomSheetNormalPoint(destination: des);
            }
             */
            showModalBottomSheet(
                constraints:
                    BoxConstraints.loose(Size(Get.width, Get.height * 0.75)),
                isScrollControlled: true,
                context: context,
                builder: ((context) => bottomSheet)
            );
          },

          suggestionsCallback: (pattern) async {
            return searchController.searchResults
                .where((GeoJSONFeature element) => element
                    .properties!["location_name"]
                    .toString()
                    .contains(pattern))
                .toList();
            //return await
          },
          itemBuilder: (context, GeoJSONFeature suggestion) {
            return ListTile(
              title: Text(suggestion.properties!["location_name"]),
              subtitle: suggestion.properties!["category"] != null
                  ? Text(suggestion.properties!["category"])
                  : const Text(""),
              //leading: getImage(index),
            );
          },
        ),
      ),
    );
  }
}
