/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-11-01 10:48:14
 * @LastEditTime : 2023-06-14 16:16:50
 * @Description  : Do not edit
 */
part of '../index.dart';

/// 顶部栏
class _TopBar extends StatelessWidget {
  const _TopBar({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();
    final store = Provider.of<WorkStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;

    return Wisdom.row(
      padding: EdgeInsets.symmetric(
        horizontal: 10.pt,
      ),
      height: 44.pt,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Wisdom.row(
          onTap: () => store.toCommunityPick(),
          children: [
            Image.asset(
              'lib/assets/main/house.png',
              package: 'app_package',
              width: 28.pt,
              height: 28.pt,
            ),
            WisText(
              communityGlobalStore.data?.name ?? '选择小区',
              size: 16.pt,
            ),
            SizedBox(
              width: 5.pt,
            ),
            Wisdom.column(
              children: [
                SizedBox(
                  height: 19.pt,
                ),
                Wisdom(
                  border: Border(
                    // 四个值 top right bottom left
                    bottom: BorderSide(
                      color: Colors.transparent, // 朝上; 其他的全部透明transparent或者不设置
                      width: 5.pt,
                      style: BorderStyle.solid,
                    ),
                    right: BorderSide(
                      color: Colors.transparent, // 朝左;  把颜色改为目标色就可以了；其他的透明
                      width: 5.pt,
                      style: BorderStyle.solid,
                    ),
                    left: BorderSide(
                      color: Colors.transparent, // 朝右；把颜色改为目标色就可以了；其他的透明
                      width: 5.pt,
                      style: BorderStyle.solid,
                    ),
                    top: BorderSide(
                      color: colorScheme.grey, // 朝下;  把颜色改为目标色就可以了；其他的透明
                      width: 5.pt,
                      style: BorderStyle.solid,
                    ),
                  ),
                )
              ],
            )
          ],
        ),
        _NoticeIcon(),
      ],
    );
  }
}

class _NoticeIcon extends StatefulWidget {
  const _NoticeIcon({Key? key}) : super(key: key);

  @override
  State<_NoticeIcon> createState() => __NoticeIconState();
}

class __NoticeIconState extends State<_NoticeIcon> {
  late VideoPlayerController _controller;
  bool isNoticeRing = false;
  /// 消息列表
  List<MsgCountModel>? msgCountList = [];

  // bool get isNewNotice => pushGlobalStore.isNewNotice;

  /// 未读消息数量 return {int}
  num? get unread => msgCountList!.length == 0
      ? 0
      : msgCountList?.firstWhere((e) => e.msgType == 'myUnread').count ?? 0;
  @override
  void initState() {
    super.initState();
    getMsgcount();
    _controller = VideoPlayerController.asset(
      'lib/assets/sound/new_notice.mp3',
      package: 'app_package',
    );

    _controller.addListener(() {
      setState(() {});
    });
    // _controller.setLooping(true);
    _controller.initialize().then((_) => setState(() {}));

    // if (store.unread()! > 0 || pushGlobalStore.isNewNotice == true) {
    //   _controller.play();
    // }
  }

  ///获取未读消息数
  Future getMsgcount() async {
    var res = await MessageApi.getMsgCount();
    msgCountList = res.data ?? [];
    setState(() {});
    if (unread! > 0) {
      _controller.play();
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  void didUpdateWidget(_NoticeIcon oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<WorkStore>(context, listen: true);
    final colorScheme = WTheme.of(context).colorScheme;
    // final pushGlobalStore = GetIt.I.get<PushGlobalStore>();
    // if (pushGlobalStore.isNewNotice == true && isNoticeRing == false) {
    //   /// 播放通知音效
    //   _controller.play();
    //   isNoticeRing = true;
    //   setState(() {});
    // }
    return Observer(
      builder: (_) => Wisdom.row(
        children: [
          if (unread! > 0) ...[
            Wisdom(
              onTap: () => store.route2notice(),
              child: Stack(children: [
                Image.asset(
                  'lib/assets/main/notify.png',
                  package: 'app_package',
                  width: 28.pt,
                  height: 28.pt,
                ),
                Positioned(
                  right: 1.pt,
                  top: 1.pt,
                  child: Wisdom(
                    width: 9.pt,
                    height: 9.pt,
                    borderRadius: BorderRadius.circular(100.pt),
                    color: colorScheme.red,
                  ),
                ),
              ]),
            ),
          ] else ...[
            Wisdom(
              onTap: () => store.route2notice(),
              child: Image.asset(
                'lib/assets/main/notify.png',
                package: 'app_package',
                width: 28.pt,
                height: 28.pt,
              ),
            ),
          ],
          SizedBox(width: 5.pt),
        ],
      ),
    );
  }
}
