/*
 * @Author       : WuWei
 * @LastEditors: wuwei
 * @Date         : 2022-03-07 11:08:19
 * @LastEditTime: 2024-03-28 14:57:23
 * @Description  : 公告消息
 */
part of '../index.dart';

class _NoticeComponent extends StatelessWidget {
  const _NoticeComponent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _BuildNoticeBlock();
  }
}

class _BuildNoticeBlock extends StatelessWidget {
  const _BuildNoticeBlock({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // final colorScheme = WTheme.of(context).style.colorScheme;
    final store = Provider.of<WorkStore>(context, listen: true);

    return Wisdom.row(
      height: 46.pt,
      color: Colors.white,
      borderRadius: BorderRadius.circular(12.pt),
      margin: EdgeInsets.fromLTRB(15.pt, 5.pt, 15.pt, 15.pt),
      padding: EdgeInsets.symmetric(horizontal: 15.pt),
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Wisdom(
          padding: EdgeInsets.only(right: 15.pt),
          // border: Border(
          //   right: BorderSide(
          //     color: colorScheme.divider,
          //     width: 0.5.pt,
          //   ),
          // ),
          child: Image(
            image: AssetList.$notice_text_png_image,
            height: 14.pt,
            width: 34.pt,
          ),
        ),

        /// 轮播消息
        if (store.noticeList.length > 0) ...[
          _BuildNotice(
            count: store.noticeList.length,
            itemBuilder: (BuildContext context, int index) {
              var item = store.noticeList[index];
              return Wisdom.column(
                onTap: () => store.toNoticeList(),
                padding: EdgeInsets.only(
                  left: 15.pt,
                ),
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Wisdom.row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Text.rich(
                        TextSpan(children: [
                          if (item.isStick == true)
                            WidgetSpan(
                              child: Wisdom(
                                borderRadius: BorderRadius.circular(20.pt),
                                padding: EdgeInsets.symmetric(vertical: 3.pt, horizontal: 8.pt),
                                color: Color(0xffFFB400),
                                child: WisText(
                                  '置顶',
                                  size: 11.pt,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                          TextSpan(
                            text: item?.title ?? '',
                            style: TextStyle(
                              fontSize: 15.pt,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ]),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ).asExpanded(),
                    ],
                  ),
                ],
              );
            },
          ).asExpanded(),
        ] else ...[
          Wisdom(
            margin: EdgeInsets.only(left: 10.pt),
            child: WisText(
              '暂无公告',
              size: 13.pt,
            ),
          ).asExpanded(),
        ],
        Wisdom(
          onTap: () => store.toNoticeList(),
          child: Image.asset(
            'lib/assets/main/enter.png',
            package: 'app_package',
            width: 14.pt,
            height: 14.pt,
          ),
        ),
      ],
    );
  }
}

class _BuildNotice extends StatefulWidget {
  _BuildNotice({Key? key, required this.count, required this.itemBuilder})
      : super(key: key);
  final int count; // 子视图数量
  final IndexedWidgetBuilder itemBuilder; // 子视图构造器

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

class _BuildNoticeState extends State<_BuildNotice> {
  late PageController _controller;
  late Timer _timer;

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

    _controller = PageController();
    _timer = Timer.periodic(Duration(seconds: 6), (timer) {
      // 如果当前位于最后一页，则直接跳转到第一页，两者内容相同，跳转时视觉上无感知
      if ((_controller.page ?? 0) >= widget.count) {
        _controller.jumpToPage(0);
      }
      if (widget.count > 0 && _controller.page != null) {
        _controller.nextPage(
            duration: Duration(seconds: 1), curve: Curves.linear);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
      scrollDirection: Axis.vertical,
      controller: _controller,
      itemBuilder: (buildContext, index) {
        if (index < widget.count) {
          return widget.itemBuilder(buildContext, index);
        } else {
          return widget.itemBuilder(buildContext, 0);
        }
      },
      itemCount: widget.count + 1, // 在原数据末尾添加一笔数据(即第一笔数据)，用于实现无限循环滚动效果
    );
  }

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