import 'dart:async';

import 'package:cashier_reserve/common/base/ui.dart';
import 'package:cashier_reserve/common/channel/model/call_status_change_model.dart';
import 'package:cashier_reserve/common/manager/event_manager.dart';

class CallView extends StatefulWidget {
  final CallStatusChangeModel statusModel;
  final Function(String action)? onAction;

  const CallView({super.key, required this.statusModel, this.onAction});

  @override
  State<StatefulWidget> createState() {
    return _CallViewState();
  }
}

class _CallViewState extends State<CallView> {
  int _callDuration = 0;
  Timer? _callDurationTimer;

  bool _isAccept = false;

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

    EventManager.addListener<CallReceivedEvent>(this, (event) {
      _acceptCall();
    });
  }

  @override
  void dispose() {
    _cancelDurationTimer();
    EventManager.cancelListener(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      child: Scaffold(
        backgroundColor: Colors.black.withOpacity(0.2),
        body: GestureDetector(
          onTap: () {
            FocusScope.of(context).requestFocus(FocusNode());
          },
          child: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            color: const Color(0x55000000),
            child: Center(
              child: Container(
                width: MediaQuery.of(context).size.width * 0.8,
                height: MediaQuery.of(context).size.height * 0.8,
                decoration: BoxDecoration(
                  color: Colors.blue.withOpacity(0.9),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: _buildCallInfo(context),
              ),
            ),
          ),
        ),
      ),
    );
  }

  String _getCallDuration() {
    // 将秒数转换为时分秒
    int hour = _callDuration ~/ 3600;
    int minute = _callDuration % 3600 ~/ 60;
    int second = _callDuration % 60;
    return "${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}:${second.toString().padLeft(2, '0')}";
  }

  _acceptCall() {
    if (_isAccept) {
      return;
    }
    _isAccept = true;
    _startDurationTimer();
  }

  _startDurationTimer() {
    _callDurationTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        _callDuration++;
      });
    });
  }

  _cancelDurationTimer() {
    _callDurationTimer?.cancel();
  }

  Widget _buildCallInfo(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        _buildIncomingInfo(context),
        const SizedBox(height: 70),
        _buildActionBtn(context),
      ],
    );
  }

  Widget _buildIncomingInfo(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          "${widget.statusModel.number}",
          style: const TextStyle(
              fontSize: 35, color: Colors.white, fontWeight: FontWeight.bold),
        ),
        const SizedBox(height: 10),
        Text(
          "${widget.statusModel.region}",
          style: const TextStyle(fontSize: 15, color: Colors.white),
        ),
        const SizedBox(height: 20),
        Text(_getCallDuration(),
            style: const TextStyle(fontSize: 20, color: Colors.white)),
      ],
    );
  }

  Widget _buildActionBtn(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        InkWell(
          onTap: () {
            // 拒绝
            if (widget.onAction != null) {
              widget.onAction!("reject");
            }
          },
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Image.asset('images/call/reject.png', width: 60, height: 60),
          ),
        ),
        if (!_isAccept) const SizedBox(width: 80),
        if (!_isAccept) InkWell(
          onTap: () {
            // 接听
            if (widget.onAction != null) {
              widget.onAction!("accept");
            }

            _acceptCall();
          },
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Image.asset('images/call/accept.png', width: 60, height: 60),
          ),
        ),
      ],
    );
  }
}
