import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'dart:math';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  MethodChannel? _methodChannel;

  void onViewCreated(int viewId) {
    _methodChannel = MethodChannel("atmob_map_method_channel");
    _methodChannel?.setMethodCallHandler((call) {
      if (call.method == "marker#onTap") {
        debugPrint("Marker tapped: ${call.arguments}");
      }
      return Future.value(null);
    });
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Stack(
        children: [
          UiKitView(
            viewType: "com.atmob.flutter.map",
            onPlatformViewCreated: onViewCreated
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Column(
              children: [
                CupertinoButton(
                  child: Text("Random Position"),
                  onPressed: () {
                    _methodChannel?.invokeMethod("map#moveCamera", {
                      "latitude": Random().nextDouble() * (40.0 - 22.0) + 22.0,
                      "longitude": Random().nextDouble() * (120.0 - 110.0) + 110.0,
                      "zoom": 0.05
                    });
                  }),
                CupertinoButton(
                  child: Text("Add Marker"),
                  onPressed: () {
                    _methodChannel?.invokeMethod("marker#updateOrAddMarkers", {
                      "id": "marker_${Random().nextInt(1000000)}",
                      "latitude": Random().nextDouble() * (40.0 - 22.0) + 22.0,
                      "longitude": Random().nextDouble() * (120.0 - 110.0) + 110.0,
                      "markerName": "Marker ${Random().nextInt(1000000)}",
                      "markerType": 1,
                      "isSelected": false
                   });    
                  }
                ),
                CupertinoButton(
                  child: Text("Clear Markers"),
                  onPressed: () {
                    _methodChannel?.invokeMethod("map#clear");
                  }
                ),
                CupertinoButton(
                  child: Text("Add Polyline"),
                  onPressed: () {
                    final points = List<Map<String, double>>.generate(20, (_) => {
                      "latitude": Random().nextDouble() * (40.0 - 22.0) + 22.0,
                      "longitude": Random().nextDouble() * (120.0 - 110.0) + 110.0
                    });
                    _methodChannel?.invokeMethod("polyline#addPolyline", {
                      "points": points,
                      "mapPadding": {
                        "top": 10,
                        "left": 10,
                        "bottom": 600,
                        "right": 10
                      }
                    });
                    // start marker
                    _methodChannel?.invokeMethod("marker#updateOrAddMarkers", {
                      "id": "marker_${Random().nextInt(1000000)}",
                      "latitude": points[0]["latitude"],
                      "longitude": points[0]["longitude"],
                      "markerName": "",
                      "markerType": 3,
                      "isSelected": false
                    });
                    // end marker
                    _methodChannel?.invokeMethod("marker#updateOrAddMarkers", {
                      "id": "marker_${Random().nextInt(1000000)}",
                      "latitude": points[points.length - 1]["latitude"],
                      "longitude": points[points.length - 1]["longitude"],
                      "markerName": "Marker ${Random().nextInt(1000000)}",
                      "markerType": 4,
                      "isSelected": false
                    });
                  }
                )
              ],
            )
          )
        ],
      ),
    );
  }
}