// lib/screens/map_screen.dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

class MapScreen extends StatefulWidget {
  @override
  _MapScreenState createState() => _MapScreenState();
}

class _MapScreenState extends State<MapScreen> {
  late GoogleMapController mapController;

  final LatLng _initialPosition = const LatLng(45.521563, -122.677433);

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
  }

  final List<Map<String, String>> _dentists = [
    {
      'name': '굿모닝 치과',
      'phone': '123-456-7890',
      'image': 'assets/images/building.jpg', // 해당 경로에 이미지 추가 필요
    },
    {
      'name': '연세치과의원',
      'phone': '987-654-3210',
      'image': 'assets/images/building.jpg', // 해당 경로에 이미지 추가 필요
    },
    {
      'name': '이사랑 치과',
      'phone': '555-555-5555',
      'image': 'assets/images/building.jpg', // 해당 경로에 이미지 추가 필요
    },
  ];

  void _launchPhone(String phoneNumber) async {
    final Uri launchUri = Uri(
      scheme: 'tel',
      path: phoneNumber,
    );
    if (await canLaunch(launchUri.toString())) {
      await launch(launchUri.toString());
    } else {
      throw 'Could not launch $phoneNumber';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('주변 치과 찾기'),
        backgroundColor: Colors.blueAccent[700],
      ),
      body: Column(
        children: [
          Expanded(
            flex: 2,
            child: GoogleMap(
              onMapCreated: _onMapCreated,
              initialCameraPosition: CameraPosition(
                target: _initialPosition,
                zoom: 11.0,
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: ListView.builder(
              itemCount: _dentists.length,
              itemBuilder: (context, index) {
                return ListTile(
                  leading: CircleAvatar(
                    backgroundImage: AssetImage(_dentists[index]['image']!),
                  ),
                  title: Text(_dentists[index]['name']!),
                  trailing: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      IconButton(
                        icon: Icon(Icons.phone),
                        onPressed: () =>
                            _launchPhone(_dentists[index]['phone']!),
                      ),
                      IconButton(
                        icon: Icon(Icons.schedule),
                        onPressed: () {
                          // 스케줄 추가 페이지로 이동하는 코드 추가
                        },
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
