import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:get/get.dart';
import 'package:rogapp/pages/index/index_controller.dart';


class PerfectureWidget  extends StatefulWidget {

  IndexController indexController;
  MapController mapController;


  PerfectureWidget({Key? key, required this.indexController, required this.mapController}) : super(key: key){
     //indexController.zoomtoMainPerf("9", mapController);
  }

  @override
  State<PerfectureWidget> createState() => _PerfectureWidgetState();
}

class _PerfectureWidgetState extends State<PerfectureWidget> {

  @override
  void initState() {
    super.initState();
  }


  bool isNumeric(String s) {
    if (s == null) {
      return false;
    }
    return double.tryParse(s) != null;
  }
   

  List<DropdownMenuItem<String>> getDropdownItems() {
    List<DropdownMenuItem<String>> dropDownItems = [];

    for (Map<String, dynamic> currency in widget.indexController.perfectures[0]) {
      //print(currency["id"].toString());
      var newDropdown = DropdownMenuItem(
        child: Text(currency["adm1_ja"].toString()),
        value: currency["id"].toString(),
      );

      dropDownItems.add(newDropdown);
    }
    return dropDownItems;
  }

  List<DropdownMenuItem<String>> getSubDropdownItems() {
    List<DropdownMenuItem<String>> dropDownItems = [];

    if(widget.indexController.subPerfs.isNotEmpty){

      for (Map<String, dynamic> currency in widget.indexController.subPerfs[0]) {
        var newDropdown = DropdownMenuItem(
          child: Text(currency["adm2_ja"].toString()),
          value: currency["id"].toString(),
        );
        dropDownItems.add(newDropdown);
      }
    }
    return dropDownItems;
  }

  List<DropdownMenuItem<String>> getCustomArea(){
    List<DropdownMenuItem<String>> dropDownItems = [];
    if(widget.indexController.areas.isNotEmpty){

      for (Map<String, dynamic> currency in widget.indexController.areas[0]) {
        var newDropdown = DropdownMenuItem(
          child: Text(currency["area_nm"].toString()),
          value: currency["id"].toString(),
        );
        dropDownItems.add(newDropdown);
        
      }
      
    }

    if(widget.indexController.customAreas.isNotEmpty){

      for (Map<String, dynamic> currency in widget.indexController.customAreas[0]) {
        var newDropdown = DropdownMenuItem(
          child: Text(currency["event_name"].toString()),
          value: currency["event_name"].toString(),
        );

        dropDownItems.add(newDropdown);
      }
      
    }

    return dropDownItems;
  }

  List<DropdownMenuItem<String>> getCategory(){
    List<DropdownMenuItem<String>> dropDownItems = [];

    dropDownItems.clear();

    //print("--------cats ------############### ${widget.indexController.cats.toString()} -------------");
    for(dynamic d in widget.indexController.cats){
      //print("-------- ddd ------############### ${d} --------dddd-----");
      var newDropdown = DropdownMenuItem(child: Text(d['category'].toString()), value: d['category'].toString());
        //print("--------cats ------############### ${d['category'].toString()} -------------");
        dropDownItems.add(newDropdown);
    }

    //return [];

    return dropDownItems;
  }

  @override
  Widget build(BuildContext context) {

    return Obx(() =>
      Row(
        children: [
          DropdownButton<String>(
      value: widget.indexController.dropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String? newValue) {
        //setState(() {
          if(newValue != null){
            widget.indexController.is_loading.value = true;
            widget.indexController.dropdownValue = newValue;
            widget.indexController.populateForPerf(newValue, widget.mapController);
          }
        //});
      },
        items: getDropdownItems()
      ),
      
      // Gifu areas
      widget.indexController.areas.isNotEmpty ? 
      DropdownButton<String>(
        value: widget.indexController.areaDropdownValue,
        icon: const Icon(Icons.arrow_downward),
        elevation: 16,
        style: const TextStyle(color: Colors.deepPurple),
          hint: const Text("select"),
          underline: Container(
            height: 2,
            color: Colors.deepPurpleAccent,
            ),
        onChanged: (String? newValue) {
        if (isNumeric(newValue!)){
         widget.indexController.is_custom_area_selected.value = false;
        }
        else{
          widget.indexController.loadCustomLocation(newValue);
          widget.indexController.is_custom_area_selected.value = true;
          widget.indexController.subPerfs.clear();
          widget.indexController.cats.clear();
        }
        setState(() {
            widget.indexController.locations.clear();
            widget.indexController.is_loading.value = true;
            widget.indexController.areaDropdownValue = newValue;
            widget.indexController.populateSubPerForArea(newValue, widget.mapController);
          });
        },
        items: getCustomArea(),
      ): const Text(""),
      
      widget.indexController.subPerfs.isNotEmpty ?
      DropdownButton<String>(
      value: widget.indexController.subDropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.deepPurple),
      hint: const Text("select"),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String? newValue) {
        setState(() {
          if(newValue != null){
            //widget.indexController.is_loading.value = true;
            //widget.indexController.populateForSubPerf(newValue, widget.mapController);
            //widget.indexController.loadLocationforSubPerf(newValue, widget.mapController);
            widget.indexController.subDropdownValue = newValue;
            widget.indexController.loadCatForCity(newValue);
          }
        });
      },
      items:
        getSubDropdownItems()
      ) :
      const Text(""),
       //CatWidget(indexController: widget.indexController,),
      widget.indexController.cats.isNotEmpty ?
      DropdownButton<String>(
        value: widget.indexController.getCatText(),
        icon: const Icon(Icons.arrow_downward),
        elevation: 16,
        style: const TextStyle(color: Colors.deepPurple),
        hint: const Text("select"),
        underline: Container(
          height: 2,
          color: Colors.deepPurpleAccent,
        ),
        onChanged: (String? newValue) {
          setState(() {
            if(newValue != null){
              widget.indexController.is_loading.value = true;
              widget.indexController.cateogory = newValue;
              widget.indexController.currentCat.clear();
              widget.indexController.currentCat.add(newValue);
              widget.indexController.populateForSubPerf(widget.indexController.subDropdownValue, widget.mapController);
              //widget.indexController.loadLocationforSubPerf(newValue, widget.mapController);
              //widget.indexController.subDropdownValue = newValue;
            }
          });
        },
        items:
          getCategory(),
      )
      :
      Container(),

        ],
      ),
    );
  }
}
