import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../view_models/sass_news_store.dart';
part 'components/swiper.dart';

class SaasNewsIndexPage extends StatelessWidget {
  const SaasNewsIndexPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => SassNewsStore(context),
      child: _PageView(),
      lazy: false,
    );
  }
}

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  @override
  Widget build(BuildContext context) {
    final store = Provider.of<SassNewsStore>(context, listen: true);
    final appGlobalStore = GetIt.I.get<AppGlobalStore>();
    final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        backgroundColor: Color(0xffDDEEFE),
        toolbarHeight: 0,
        automaticallyImplyLeading: false,
      ),
      body: Stack(children: [
        Image.asset(
          'lib/assets/bg.png',
          package: 'news_package',
          width: MediaQuery.of(context).size.width,
        ),
        Observer(builder: (_) {
          return Column(children: [
            Wisdom.row(
              margin: EdgeInsets.only(top: 15.pt, bottom: 18.5.pt),
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                WisText(
                  '绘资讯',
                  size: 19.pt,
                  weight: FontWeight.bold,
                  color: colorScheme.mainTitle,
                ),
              ],
            ),
            if (communityGlobalStore.data?.id == null) ...[
              Wisdom.column(
                mainAxisAlignment: MainAxisAlignment.center,
                margin: EdgeInsets.only(bottom: 10.pt),
                padding: EdgeInsets.symmetric(vertical: 50.pt),
                children: [
                  WEmptyWidget(
                    text: Text('空空如也~请先选择小区再进行查看哦~'),
                  ),
                ],
              ).asFlexible(flex: 1),
            ] else ...[
              Observer(builder: (_) {
                return WisScrollView(
                  key: appGlobalStore.scorllViewKey,
                  onRefresh: () => store.initList(),
                  onInit: () => store.initList(),
                  onLoad: () => store.initList(),
                  emptyWidget: SliverToBoxAdapter(
                    child: Wisdom.column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      margin: EdgeInsets.only(bottom: 10.pt),
                      padding: EdgeInsets.symmetric(vertical: 50.pt),
                      children: [
                        WEmptyWidget(
                          text: Text('空空如也~暂无发布内容哦~'),
                        ),
                      ],
                    ),
                  ),
                  slivers: <Widget>[
                    if (appGlobalStore.picList.length > 0) ...[
                      _SwiperComponent(),
                    ],
                    if (appGlobalStore.sassNewslist.length > 0) ...[
                      _ListComponent(),
                    ],
                  ],
                ).asFlexible(flex: 1);
              }),
              Container(height: 30.pt),
            ],
            Opacity(
              opacity: 1,
              child: Container(
                color: Colors.transparent,
                height: 50.pt,
              ),
            ),
          ]);
        })
      ]),
    );
  }
}

class _ListComponent extends StatelessWidget {
  const _ListComponent({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    final store = Provider.of<SassNewsStore>(context);
    final appGlobalStore = GetIt.I.get<AppGlobalStore>();
    return WisSliverList.childBuilder(
      childCount: appGlobalStore.sassNewslist.length,
      builder: (_, i) {
        var item = appGlobalStore.sassNewslist[i];
        return InkWell(
          onTap: () => store.route2detail(item, 'list'),
          borderRadius: BorderRadius.circular(5.pt),
          child: WisCard(
              margin: EdgeInsets.only(left: 15.pt, right: 15.pt, top: 10.pt),
              color: Colors.white,
              child: Column(children: [
                Wisdom.row(children: [
                  Container(width: 15.pt),
                  Wisdom(
                    child: WImage.network(
                      (UrlFunc.urlHasMatch(item.thumb!)
                          ? item.thumb
                          : AppParseUtil.image('${item.thumb}'))!,
                      height: 70.pt,
                      width: 70.pt,
                      borderRadius: BorderRadius.circular(4.0),
                    ),
                    margin: EdgeInsets.only(top: 15.pt),
                  ),
                  Wisdom.column(
                    padding: EdgeInsets.fromLTRB(15.pt, 15.pt, 15.pt, 0),
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 10.pt),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Expanded(
                              child: WisText.c000(item.title ?? '',
                                  size: 15.pt, bold: true),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Expanded(
                              child: WisText.c000(
                                item.summary ?? '',
                                size: 13.pt,
                                color: Color.fromRGBO(102, 102, 102, 1),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ).asExpanded(),
                  if (item.informationType == 2 ||
                      (item.informationType == 1 &&
                          item.jumpLink != null &&
                          item.jumpLink != '')) ...[
                    Icon(Icons.chevron_right, color: Color(0xff1E95FF)),
                    Container(width: 15.pt),
                  ]
                ]),
                Divider(height: 20.pt),
                Wisdom.row(
                  padding: EdgeInsets.fromLTRB(15.pt, 0, 15.pt, 10.pt),
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    WisText(
                      '发布时间：${item.publishTime ?? '--'}',
                      color: colorScheme.minor,
                      size: 13.pt,
                    ),
                    WisText(
                      '阅读数：${item.viewCount ?? '--'}',
                      color: colorScheme.minor,
                      size: 13.pt,
                    ),
                  ],
                ),
              ])),
        );
      },
    );
  }
}
