import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:gifunavi/pages/destination/destination_controller.dart';
import 'package:gifunavi/routes/app_pages.dart'; // これを追加

class CurrentPosition extends StatefulWidget {
  const CurrentPosition({super.key});

  @override
  State<CurrentPosition> createState() => _CurrentPositionState();
}

class _CurrentPositionState extends State<CurrentPosition> {
  final DestinationController destinationController =
      Get.find<DestinationController>();

  void _onLongPress() async {
    PermissionStatus status = await Permission.location.status;
    if (!status.isGranted) {
      await showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: const Text('位置情報の許可が必要です'),
            content: const Text('現在位置を表示するには、位置情報の許可が必要です。「設定」からアプリの権限を許可してください。'),
            actions: [
              TextButton(
                child: const Text('キャンセル'),
                onPressed: () => Navigator.of(context).pop(),
              ),
              TextButton(
                child: const Text('設定'),
                onPressed: () {
                  Navigator.of(context).pop();
                  openAppSettings();
                },
              ),
            ],
          );
        },
      );
    } else {
      Get.toNamed(AppPages.SETTINGS);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector( // GestureDetectorを追加
      onLongPress: _onLongPress, // 長押しイベントを追加
      child: Container (
//    return Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
            color: Colors.grey, borderRadius: BorderRadius.circular(20.0)),
        child: IconButton(
          onPressed: () {
            destinationController.centerMapToCurrentLocation();
          },
          icon: const Icon(
            Icons.location_searching,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}
