import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/sc_debounce_widget.dart';
import 'package:yumi/ui_kit/components/sc_compontent.dart';
import 'package:yumi/ui_kit/components/text/sc_text.dart';
import 'package:yumi/shared/tools/sc_room_utils.dart';
import 'package:yumi/shared/data_sources/models/message/sc_floating_message.dart';
import 'package:yumi/main.dart';

class InviteRoomDialog extends StatefulWidget {
  SCFloatingMessage msg;

  InviteRoomDialog(this.msg);

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

class _InviteRoomDialogState extends State<InviteRoomDialog> {
  Timer? _timer;
  int countdown = 10;

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

  /// 启动倒计时
  void _startTimer() {
    // 计时器（`Timer`）组件的定期（`periodic`）构造函数，创建一个新的重复计时器。
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (countdown == 0) {
        _cancelTimer();
        SmartDialog.dismiss(tag: "showInviteRoom");
        return;
      }
      countdown--;
      setState(() {});
    });
  }

  /// 取消倒计时的计时器。
  void _cancelTimer() {
    // 计时器（`Timer`）组件的取消（`cancel`）方法，取消计时器。
    _timer?.cancel();
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().screenWidth * 0.8,
      padding: EdgeInsets.all(15.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(12.w)),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: 3.w),
          Row(
            children: [
              Spacer(),
              text("$countdown", textColor: Colors.black87, fontSize: 14.sp),
              SizedBox(width: 3.w),
              GestureDetector(
                child: Image.asset(
                  "sc_images/general/sc_icon_clear_c.png",
                  width: 18.w,
                ),
                onTap: () {
                  SmartDialog.dismiss(tag: "showInviteRoom");
                },
              ),
            ],
          ),
          netImage(
            url: widget.msg.userAvatarUrl ?? "",
            width: 80.w,
            height: 80.w,
            shape: BoxShape.circle,
          ),
          SizedBox(height: 10.w),
          Row(
            children: [
              Expanded(
                child: text(
                  SCAppLocalizations.of(context)!.inviteGoRoomTips,
                  textColor: Colors.black87,
                  fontSize: 15.sp,
                  maxLines: 5,
                ),
              ),
            ],
          ),
          SizedBox(height: 10.w),
          SCDebounceWidget(
            child: Container(
              height: 45.w,
              margin: EdgeInsets.symmetric(horizontal: 35.w),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage("sc_images/room/sc_icon_inv_go_btn.png"),
                  fit: BoxFit.fill,
                ),
              ),
              child: text(
                SCAppLocalizations.of(context)!.enterTheRoom,
                textColor: Colors.white,
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
              ),
            ),
            onTap: () {
              SmartDialog.dismiss(tag: "showInviteRoom");
              SCRoomUtils.goRoom(widget.msg.roomId ?? "", navigatorKey.currentState!.context);
            },
          ),
        ],
      ),
    );
  }
}

class _timer {}
