import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import '../../../core/constants/app_assets.dart';
import '../../../models/Branch.dart';


class AgentLocationController extends GetxController {
  final TextEditingController searchController = TextEditingController();

  BuildContext context = Get.context as BuildContext;
  final RxSet<Marker> markers = <Marker>{}.obs;
  final List<String> mapType = ['Normal', 'Satellite', 'Hybrid'].obs;
  BitmapDescriptor? myMarker;
  Rx<MapType> selectedMapType = MapType.normal.obs;

  /// Change Map Type
  void changeMapType(MapType newMapType) {
    selectedMapType.value = newMapType;
  }

  @override
  void onInit() {
    super.onInit();

    /// Initialize with some default markers
    searchBranches('Normal');
  }

  void searchBranches(String branchName) {
    /// Simulate fetching branch locations based on the city
    /// Replace this with your actual logic to get branch locations
    List<Branch> branches = getBranches(branchName);

    /// Clear existing markers
    markers.clear();

    /// Add markers for each branch in the selected city
    if (branches.isNotEmpty) {
      branches.forEach((branch) async {
        markers.add(
          Marker(
            markerId: MarkerId(branch.name),
            position: LatLng(branch.latitude, branch.longitude),
            icon: await BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),

            onTap: () {
              showCustomInfoWindow(branch);
            },
            // infoWindow: InfoWindow(
            //   title: branch.name,
            //   snippet: branch.contactNumber,
            // ),
          ),
        );
      });
      update();
    }
  }

  /// Custom Info Window for marker
  void showCustomInfoWindow(Branch branch) {
    Clipboard.setData(ClipboardData(text: branch.name));
    Clipboard.setData(ClipboardData(text: branch.contactNumber));
    Clipboard.setData(ClipboardData(text: branch.address));
    Get.defaultDialog(
      contentPadding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
      title: branch.name,
      titlePadding: const EdgeInsets.only(top: 10),
      titleStyle: Theme.of(context).textTheme.titleSmall!.copyWith(fontWeight: FontWeight.w500),
      content: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                AppAssets.lg_mfsys, // Replace with your logo image path
                width: 35,
                height: 35,
              ),
              const SizedBox(width: 10),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Row(
                    children: [
                      const Icon(
                        Icons.location_on,
                        size: 13,
                        color: Colors.blue,
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 10.0),
                        child: Text('MFSYS Technologies', style: Theme.of(context).textTheme.bodySmall!.copyWith(fontSize: 10, fontWeight: FontWeight.w400)),
                      ),
                    ],
                  ),
                  const SizedBox(height: 10),
                  Row(
                    children: [
                      const Icon(
                        Icons.phone, // Replace with your desired icon
                        size: 13,
                        color: Colors.blue,
                      ),
                      const SizedBox(height: 8),
                      Padding(
                        padding: const EdgeInsets.only(left: 10.0),
                        child: Text(
                          style: Theme.of(context).textTheme.bodySmall!.copyWith(fontSize: 10, fontWeight: FontWeight.w400),
                          '(051) 41021712',
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }

  /// Branches List
  List<Branch> getBranches(String cityName) {
    return [
      Branch(name: 'MFSYS Technolog', latitude: 37.7749, longitude: -122.4194, contactNumber: '123-456-7890', address: "Main Branch I9/3"),
      Branch(name: 'MFSYS Technolog', latitude: 37.7831, longitude: -122.4039, contactNumber: '987-654-3210', address: "Main Branch I9/3"),
      Branch(name: 'MFSYS Technolog', latitude: 37.7841, longitude: -122.4539, contactNumber: '987-654-3210', address: "Main Branch I9/3"),
      Branch(name: 'Branch E', latitude: 37.7867, longitude: -122.4089, contactNumber: '987-654-3210', address: "Main Branch I9/3"),
    ];
  }
}
