import 'package:cpnav/pages/pass_track/view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:scence_map/controller.dart';

ScenceMapController mapController = Get.put(ScenceMapController());

class IconContainer extends StatefulWidget {
  @override
  _IconContainerState createState() => _IconContainerState();
}

class _IconContainerState extends State<IconContainer> {
  final Map<IconData, Color> _iconColors = {
    Icons.article: Colors.green,
    Icons.speed: const Color.fromARGB(255, 166, 182, 149),
    Icons.thermostat: const Color.fromARGB(255, 166, 182, 149),
    Icons.zoom_in: const Color.fromARGB(255, 166, 182, 149),
    Icons.zoom_out: const Color.fromARGB(255, 166, 182, 149),
    Icons.refresh: const Color.fromARGB(255, 166, 182, 149),
    Icons.place: const Color.fromARGB(255, 166, 182, 149),
  };
  IconData? _selectedIcon;

  void _handleTap1(IconData icon, VoidCallback onTap) {
    setState(() {
      _selectedIcon = icon;
      _iconColors.forEach((key, value) {
        _iconColors[key] = key == icon
            ? Colors.green // 点击后变浅的颜色
            : const Color.fromARGB(255, 166, 182, 149); // 其他图标恢复原色
      });
    });
    onTap();
  }

  void _handleTap2(IconData icon, VoidCallback onTap) {
    setState(() {
      _iconColors[icon] = Colors.green; // 点击后变浅的颜色
    });
    onTap();
    Future.delayed(Duration(milliseconds: 200), () {
      setState(() {
        _iconColors[icon] = const Color.fromARGB(255, 166, 182, 149); // 恢复原色
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    var isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;
    return isPortrait
        ? Positioned(
            right: 10,
            bottom: 130,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  width: 42,
                  decoration: BoxDecoration(
                    color: Colors.transparent,
                    border: Border.all(
                      color: const Color.fromARGB(255, 54, 52, 52),
                      width: 1.0,
                    ),
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                  child: Column(
                    children: [
                      _singleChange(Icons.article, () {}),
                      _singleChange(Icons.speed, () {
                        // 处理 speed 图标的点击事件
                      }),
                      _singleChange(Icons.thermostat, () {
                        // 处理 thermostat 图标的点击事件
                      }),
                      _automChange(Icons.zoom_in, () {
                        mapcontroller.scale = mapcontroller.scale * 0.5;
                        if (mapcontroller.scale < 0.2) {
                          mapcontroller.scale = 0.2;
                          const snackBar = SnackBar(
                            content: Text("当前已到放大比例极限"),
                            backgroundColor: Colors.red,
                            duration: Duration(seconds: 1),
                          );

                          ScaffoldMessenger.of(context).showSnackBar(snackBar);
                        }

                        mapcontroller.rotation.value = 0.0;
                        mapcontroller.updateCount.value++;
                      }),
                      _automChange(Icons.zoom_out, () {
                        mapcontroller.scale = mapcontroller.scale / 0.5;

                        if (mapcontroller.scale > 200) {
                          mapcontroller.scale = 200;
                          // 提示最大比例
                          const snackBar = SnackBar(
                            content: Text("当前已到缩小比例极限"),
                            backgroundColor: Colors.red,
                            duration: Duration(seconds: 1),
                          );

                          ScaffoldMessenger.of(context).showSnackBar(snackBar);
                        }

                        mapcontroller.rotation.value = 0.0;
                        mapcontroller.updateCount.value++;
                      }),
                      _automChange(Icons.refresh, () {
                        mapcontroller.scale = 1.0;
                        mapcontroller.rotation.value = 0.0;
                        mapcontroller.updateCount.value++;
                      }),
                      _automChange(Icons.place, () {
                        // 处理 place 图标的点击事件
                      }),
                    ],
                  ),
                )
              ],
            ))
        : Positioned(
            right: 10,
            bottom: 15,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  height: 42,
                  decoration: BoxDecoration(
                    color: Colors.transparent,
                    border: Border.all(
                      color: const Color.fromARGB(255, 54, 52, 52),
                      width: 1.0,
                    ),
                    borderRadius: BorderRadius.circular(5.0),
                  ),
                  child: Row(
                    children: [
                      _singleChange(Icons.article, () {
                        mapcontroller.scale = 1.0;
                        mapcontroller.rotation.value = 0.0;
                        mapcontroller.updateCount.value++;
                      }),
                      _singleChange(Icons.speed, () {
                        // 处理 speed 图标的点击事件
                      }),
                      _singleChange(Icons.thermostat, () {
                        // 处理 thermostat 图标的点击事件
                      }),
                      _automChange(Icons.zoom_in, () {
                        mapcontroller.scale = mapcontroller.scale * 0.5;
                        if (mapcontroller.scale < 0.2) {
                          mapcontroller.scale = 0.2;
                          const snackBar = SnackBar(
                            content: Text("当前已到放大比例极限"),
                            backgroundColor: Colors.red,
                            duration: Duration(seconds: 1),
                          );

                          ScaffoldMessenger.of(context).showSnackBar(snackBar);
                        }

                        mapcontroller.rotation.value = 0.0;
                        mapcontroller.updateCount.value++;
                      }),
                      _automChange(Icons.zoom_out, () {
                        mapcontroller.scale = mapcontroller.scale / 0.5;

                        if (mapcontroller.scale > 200) {
                          mapcontroller.scale = 200;
                          // 提示最大比例
                          const snackBar = SnackBar(
                            content: Text("当前已到缩小比例极限"),
                            backgroundColor: Colors.red,
                            duration: Duration(seconds: 1),
                          );

                          ScaffoldMessenger.of(context).showSnackBar(snackBar);
                        }

                        mapcontroller.rotation.value = 0.0;
                        mapcontroller.updateCount.value++;
                      }),
                      _automChange(Icons.refresh, () {
                        mapcontroller.scale = 1.0;
                        mapcontroller.rotation.value = 0.0;
                        mapcontroller.updateCount.value++;
                      }),
                      _automChange(Icons.place, () {
                        // 处理 place 图标的点击事件
                      }),
                    ],
                  ),
                )
              ],
            ));
  }

  //前三个单选
  Widget _singleChange(IconData icon, VoidCallback onTap) {
    return Container(
      width: 33,
      height: 33,
      margin: const EdgeInsets.symmetric(vertical: 2.0),
      decoration: BoxDecoration(
        color: _iconColors[icon],
        border: Border.all(
          color: Colors.white,
        ),
        borderRadius: BorderRadius.circular(5.0),
      ),
      child: InkWell(
        child: Icon(
          icon,
          color: Colors.white,
        ),
        onTap: () => _handleTap1(icon, onTap),
      ),
    );
  }

  //后三个自动恢复
  Widget _automChange(IconData icon, VoidCallback onTap) {
    return Container(
      width: 33,
      height: 33,
      margin: const EdgeInsets.symmetric(vertical: 2.0),
      decoration: BoxDecoration(
        color: _iconColors[icon],
        border: Border.all(
          color: Colors.white,
        ),
        borderRadius: BorderRadius.circular(5.0),
      ),
      child: InkWell(
        child: Icon(
          icon,
          color: Colors.white,
        ),
        onTap: () => _handleTap2(icon, onTap),
      ),
    );
  }
}