import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/news/model/news_comm_entity.dart';
import 'package:liftmanager/internal/news/presenter/news_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

import '../news_router.dart';

class NewsListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return NewsListStateList();
  }
}

class NewsListStateList
    extends BasePageState<NewsListPage, NewsListPresenterss> {
  BaseListProvider<NewsItems> provider = BaseListProvider<NewsItems>();

  String _keyword = "";
  int _page = 1;

  @override
  void initState() {
    /// 默认为加载中状态，本页面场景默认为空
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<BaseListProvider<NewsItems>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "新闻列表",
        ),
        body: Consumer<BaseListProvider<NewsItems>>(builder: (_, provider, __) {
          return MyListView(
            key: Key('order_search_list'),
            itemCount: provider.list.length,
            stateType: provider.stateType,
            onRefresh: _onRefresh,
            loadMore: _loadMore,
            hasMore: provider.hasMore,
            itemBuilder: (_, index) {
              print(
                  'kan--${provider.list[index].title} ++++${provider.list[index].cover}');
              bool isDark = ThemeUtils.isDark(context);
              return InkWell(
                onTap: () {
                  NavigatorUtils.push(context,
                      "${NewsRouter.newsDetail}?id=${provider.list[index].id}");
                },
                child: Container(
                  decoration: BoxDecoration(
                    color: ThemeUtils.getTabsBg(context),
                    border: Border(
                        bottom: BorderSide(width: 0.5, color: Colours.line)),
                  ),
                  padding: const EdgeInsets.all(15),
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Expanded(
                              child: Text(
                                "${provider.list[index].title}",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    fontSize: 14,
                                    color: isDark
                                        ? Color(0xff333333)
                                        : Colours.text),
                              ),
                            ),
                            // SizedBox(
                            //   width: 25,
                            // ),
                            provider.list[index].cover != null
                                ? Container(
                                    margin: EdgeInsets.only(left: 30, top: 2),
                                    height: 70,
                                    width: 90,
                                    child: Image.network(
                                      provider.list[index].cover ?? '',
                                      fit: BoxFit.cover,
                                    ),
                                  )
                                : Container()
                          ],
                        ),
                        Gaps.vGap12,
                        Row(
                          children: [
                            Expanded(
                              child: Text(
                                "来源：${provider.list[index].releaseUser}·${provider.list[index].releaseDate}",
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Color(0xff999999),
                                ),
                              ),
                            ),
                            Text(
                              '${provider.list[index].lookNum}浏览·${provider.list[index].likeNum}赞',
                              style: TextStyle(
                                fontSize: 12,
                                color: Color(0xffcccccc),
                              ),
                            ),
                          ],
                        ),
                      ]),
                ),
              );
            },
          );
        }),
      ),
    );
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.list(_keyword, _page, 10, 1);
  }

  Future _loadMore() async {
    _page++;
    await presenter.list(_keyword, _page, 10, 1);
  }

  @override
  NewsListPresenterss createPresenter() {
    return NewsListPresenterss();
  }
}
