import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.dart';
import 'package:liftmanager/internal/bbs/presenter/mix_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/utils/fast_notification.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';

// import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'package:provider/provider.dart';

class CollectPage extends StatefulWidget {
  String index = "1";

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

class CollectPageState extends BasePageState<CollectPage, MixListPresenter> {
  // NewsDetailItem item = NewsDetailItem();
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int checkIndex = 0;

  int _page = 1;
  List<String> titleList = [
    // "全部",
    "问答",
    "视频",
    "职位",
    "商品",
  ];

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    FastNotification.addListener("collectAction", (collectInit) {
      if (mounted) {
        setState(() {});
        _onRefresh();
      }
    });
  }

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

  @override
  void deactivate() {
    super.deactivate();
    print(123667);
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
        appBar: MyAppBar(
          centerTitle: "我的收藏",
        ),
        body: Container(
            child: Stack(
          children: <Widget>[
            Positioned(
              child: Container(
                  padding: EdgeInsets.only(bottom: 5),
                  // color:Color(0xffFAF7FA),
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(width: 0.5, color: Color(0xffeeeeee)),
                    ),
                    // color: Color(0xffffffff),
                  ),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: titleList.asMap().keys.map((i) {
                        return TabThis(
                            item: titleList[i],
                            tabIndex: i,
                            checkIndex: checkIndex,
                            fun: () {
                              provider.list.clear();
                              setState(() {
                                checkIndex = i;
                              });
                              provider.setStateTypeNotNotify(StateType.loading);
                              _onRefresh();
                              print(checkIndex);
                            });
                      }).toList())),
            ),
            Container(
                child: ChangeNotifierProvider<BaseListProvider<Records>>(
                    create: (_) => provider,
                    child: Column(
                      children: <Widget>[
                        SizedBox(height: ScreenUtil().setWidth(50)),
                        Expanded(
                            flex: 1,
                            child: Consumer<BaseListProvider<Records>>(
                                builder: (_, provider, __) {
                              return MyListView(
                                key: Key('mix_list'),
                                showSeparator: checkIndex == 0,
                                itemCount: provider.list.length,
                                stateType: provider.stateType,
                                onRefresh: _onRefresh,
                                loadMore: _loadMore,
                                hasMore: provider.hasMore,
                                padding: EdgeInsets.symmetric(horizontal: 10),
                                itemBuilder: (_, index) {
                                  return Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      checkIndex == 0
                                          ? QuestionCell(
                                              item: provider.list[index])
                                          : Container(child: null),
                                      checkIndex == 1
                                          ? VideoCell(provider.list[index])
                                          : Container(child: null),
                                      checkIndex == 2
                                          ? PositionCell(provider.list[index])
                                          : Container(child: null),
                                      checkIndex == 3
                                          ? ProductCell(provider.list[index])
                                          : Container(child: null),
                                    ],
                                  );
                                },
                              );
                            }))
                      ],
                    )))
          ],
        )));
  }

  Future _onRefresh() async {
    _page = 1;
    if (checkIndex == 0) {
      await presenter.getQuestionList(_page);
    } else if (checkIndex == 1) {
      await presenter.getVideoList(_page);
    } else if (checkIndex == 2) {
      await presenter.getPositionList(_page);
    } else if (checkIndex == 3) {
      await presenter.getShopList(_page);
    }
  }

  Future _loadMore() async {
    _page++;
    if (checkIndex == 0) {
      await presenter.getQuestionList(_page);
    } else if (checkIndex == 1) {
      await presenter.getVideoList(_page);
    } else if (checkIndex == 2) {
      await presenter.getPositionList(_page);
    } else if (checkIndex == 3) {
      await presenter.getShopList(_page);
    }
  }

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

class TabThis extends StatelessWidget {
  TabThis({Key key, this.item, this.tabIndex, this.fun, this.checkIndex})
      : super(key: key);
  String item;
  int tabIndex;
  int checkIndex;
  Function fun;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: ScreenUtil().setWidth(10)),
      child: Container(
          child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
            GestureDetector(
                onTap: () {
                  fun();
                },
                child: Container(
                  padding: EdgeInsets.only(bottom: 6),
                  decoration: BoxDecoration(
                    border: Border(
                      bottom: BorderSide(
                          width: 2,
                          color: checkIndex == tabIndex
                              ? Color(0xff02A0FD)
                              : Colors.transparent),
                    ),
                    // color: Color(0xff9FD1FE),
                  ),
                  child: Text(
                    item,
                    style: TextStyle(
                        color: checkIndex == tabIndex
                            ? Color(0xff02A0FD)
                            : Color(0xff666666),
                        fontSize: ScreenUtil().setSp(14)),
                    textAlign: TextAlign.center,
                  ),
                ))
          ])),
    );
  }
}
