import 'dart:ui';

import 'package:ef/ef.dart';
import 'package:geolocator/geolocator.dart';

import 'package:json_annotation/json_annotation.dart';
import 'package:vbvs_app/controller/weather/weather_controller.dart';
import 'package:vbvs_app/common/util/Dio.dart';

part 'experience_store_list_page.g.dart';

@JsonSerializable()
class ExperienceStoreListModel {
  List experienceStoreModelList = []; //体验店列

  @JsonKey(ignore: true)
  String? keyword = '';
  @JsonKey(ignore: true)
  Color? color = Color(0xFFFFFFFF);

  ExperienceStoreListModel();
  // static ExperienceStoreListModel fromJson(Map<String, dynamic> json) =>
  //     _$ExperienceStoreListModelFromJson(json);
  // Map<String, dynamic> toJson() => _$ExperienceStoreListModelToJson(this);
}

class ExperienceStoreListController
    extends GetControllerEx<ExperienceStoreListModel> {
  ExperienceStoreListController() {
    attr = GetModel(ExperienceStoreListModel()).obs;
  }
  WeatherModelController weatherModelController = Get.find();
  Position? position;
  double latitude = 31.8512;
  double longitude = 117.26061;

  int total = 0;
  int page = 0;
  bool lock = false;

  resetParm() {
    position = null;
    total = 0;
    page = 0;
    lock = false;
    model.experienceStoreModelList = [];
    updateAll();
  }

  getData({int count = 10}) async {
    if (page != 0 && page * count > total) {
      return;
    }
    if (lock) {
      return;
    }
    if (page == 0) {
      position = await weatherModelController.determinePosition();
    }
    lock = true;
    int page_ = page;
    ApiService.reservation
        .get(
            "/agent/userBook/config/getBookStoreList?storeName=${"${model.keyword}".isNotEmpty ? model.keyword : ''}&latitude=${position == null ? latitude : position?.latitude}&longitude=${position == null ? longitude : position?.longitude}&page=$page&size=$count&sort=distance")
        .then((d) {
      lock = false;
      if (page == 0) {
        model.experienceStoreModelList = d.data["records"];
      } else {
        int index = 0;
        d.data["records"]?.forEach((item) {
          if (model.experienceStoreModelList
                  .indexWhere((item2) => item2["id"] == item["id"]) ==
              -1) {
            model.experienceStoreModelList.add(item);
          } else {
            model.experienceStoreModelList[index] = item;
          }
          index++;
        });
      }
      page = page_ + 1;
      total = d.data["total"];
      updateAll();
    }).catchError((d) {
      lock = false;
    });
  }

  //determinePosition() async {
  // bool serviceEnabled;
  // LocationPermission permission;

  // // Test if location services are enabled.
  // serviceEnabled = await Geolocator.isLocationServiceEnabled();
  // if (!serviceEnabled) {
  //   // Location services are not enabled don't continue
  //   // accessing the position and request users of the
  //   // App to enable the location services.
  //   return Future.error('Location services are disabled.');
  // }

  // permission = await Geolocator.checkPermission();
  // if (permission == LocationPermission.denied) {
  //   permission = await Geolocator.requestPermission();
  //   if (permission == LocationPermission.denied) {
  //     // Permissions are denied, next time you could try
  //     // requesting permissions again (this is also where
  //     // Android's shouldShowRequestPermissionRationale
  //     // returned true. According to Android guidelines
  //     // your App should show an explanatory UI now.
  //     return Future.error('Location permissions are denied');
  //   }
  // }

  // if (permission == LocationPermission.deniedForever) {
  //   // Permissions are denied forever, handle appropriately.
  //   return Future.error(
  //       'Location permissions are permanently denied, we cannot request permissions.');
  // }

  // // When we reach here, permissions are granted and we can
  // // continue accessing the position of the device.
  // position = await Geolocator.getCurrentPosition();
  //}
}
