import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/utils/counter_overlay.dart';
import 'package:flutter/material.dart';

class RecordOverlay {
  static String msg;

  static OverlayEntry entry;

  static show(BuildContext context, {String msgs = ''}) {
    if (msgs == '') {
      msgs = I18n.of(context).up_cancle;
    }
    msg = msgs;
    var overlayState = Overlay.of(context);
    OverlayEntry overlayEntry;
    overlayEntry = new OverlayEntry(builder: (context) {
      return buildToastLayout();
    });
    overlayState.insert(overlayEntry);

    if (entry != null) {
      entry.remove();
    }
    entry = overlayEntry;
    return overlayEntry;
  }

  static updateMsg(String newMsg) {
    msg = newMsg;
    if (entry != null) {
      entry.markNeedsBuild();
    }
  }

  static hide() {
    print('删除overlay');
    entry?.remove();
    entry = null;
  }

  static LayoutBuilder buildToastLayout() {
    return LayoutBuilder(builder: (context, constraints) {
      if (msg == null) {
        msg = I18n.of(context).up_cancle;
      }
      return IgnorePointer(
        ignoring: true,
        child: Container(
          child: Material(
            color: Colors.white.withOpacity(0),
            child: Container(
              width: 200,
              height: 120,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  CounterOverlay(),
                  Container(
                    child: Text(
                      "$msg",
                      textScaleFactor: 1.0,
                      style: TextStyle(color: Colors.white, fontSize: 16),
                    ),
                  ),
                ],
              ),
              decoration: BoxDecoration(
                color: Constants.BlackTextColor.withOpacity(0.8),
                borderRadius: BorderRadius.all(
                  Radius.circular(5),
                ),
              ),
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
            ),
            // margin: EdgeInsets.only(
            //   bottom: constraints.biggest.height * 0.15,
            //   left: constraints.biggest.width * 0.2,
            //   right: constraints.biggest.width * 0.2,
            // ),
          ),
          alignment: Alignment.center,
        ),
      );
    });
  }
}
