import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'connection_status.dart';

class ConnectionWidget extends StatefulWidget {
  final ConnectionStatus status;
  final Function() onTap;  // 只通知外部发生了点击事件

  const ConnectionWidget({
    Key? key,
    required this.status,
    required this.onTap,
  }) : super(key: key);

  @override
  _ConnectionWidgetState createState() => _ConnectionWidgetState();
}

class _ConnectionWidgetState extends State<ConnectionWidget> {
  late String currentImage;

  @override
  void initState() {
    super.initState();
    _updateImage();
  }

  @override
  void didUpdateWidget(ConnectionWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.status != widget.status) {
      _updateImage();
    }
  }

  void _updateImage() {
    switch (widget.status) {
      case ConnectionStatus.disconnected:
        currentImage = 'assets/images/main/disconnected.gif';
        break;
      case ConnectionStatus.connecting:
        currentImage = 'assets/images/main/connecting.gif';
        break;
      case ConnectionStatus.stopped:
        currentImage = 'assets/images/main/stopped.gif';
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Image.asset(
          currentImage,
          fit: BoxFit.cover,
          width: 250,
          height: 250,
        ),
        ClipOval(
          child: Material(
            color: Colors.transparent,
            child: GestureDetector(
              onTap: widget.onTap,  // 使用外部传入的点击函数
              child: Container(
                width: 100,
                height: 100,
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.transparent,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
