import 'dart:collection';

import 'package:base_app/data/protocol/model.dart';
import 'package:base_app/data/repository/article_repository.dart';
import 'package:base_app/data/repository/common_repository.dart';
import 'package:base_app/models/article_model.dart';
import 'package:base_app/models/common/banner_model.dart';
import 'package:base_app/models/common/version_model.dart';
import 'package:base_app/res/strings.dart';
import 'package:base_app/utils/http_utils.dart';
import 'package:flustars/flustars.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:rxdart/rxdart.dart';

import 'bloc_provider.dart';

/// 列表页面bloc（list_page）
class ListBloc implements BlocBase {
  /// banner
  //banner的controller
  BehaviorSubject<List<BannerModel>> _banner =
      BehaviorSubject<List<BannerModel>>();
  // banner入口sink
  Sink<List<BannerModel>> get _bannerSink => _banner.sink;
  // banner出口stream
  Stream<List<BannerModel>> get bannerStream => _banner.stream;
  // 访问后台的repository
  CommonRepository commonRepository = new CommonRepository();

  /// 文章列表
  BehaviorSubject<List<ArticleModel>> _article =
      BehaviorSubject<List<ArticleModel>>();
  Sink<List<ArticleModel>> get _articleSink => _article.sink;
  Stream<List<ArticleModel>> get articleStream => _article.stream;
  ArticleRepository articleRepository = new ArticleRepository();
  // 构造器
  ListBloc() {
    LogUtil.e("ListBloc......");
  }

  /// 获取数据
  /// [lableId] 那个页面的标识
  /// [page] //此处无用
  @override
  Future getData({String labelId, int page}) {
    switch (labelId) {
      case Ids.titleList:
        return getHomeData(labelId);
        break;
      default:
        return Future.delayed(new Duration(seconds: 1));
        break;
    }
  }

  @override
  Future onLoadMore({String labelId}) {
    switch (labelId) {
      case Ids.titleList:
        return getHomeData(labelId);
        break;
      default:
        return Future.delayed(new Duration(seconds: 1));
        break;
    }
  }

  @override
  Future onRefresh({String labelId, bool isReload}) {
    switch (labelId) {
      case Ids.titleList:
        return getHomeData(labelId);
        break;
      default:
        return Future.delayed(new Duration(seconds: 1));
        break;
    }
  }

  Future getHomeData(String labelId) {
    getArticleList(labelId, 1);
    return getBanner(labelId);
  }

  Future getArticleList(String labelId, offset) async {
    ArticleListReq _articleReq = new ArticleListReq(offset, 10);
    articleRepository.getArticleList(_articleReq).then((list) {
      if (list.length > 6) {
        list = list.sublist(0, 6);
      }
      _articleSink.add(UnmodifiableListView<ArticleModel>(list));
    });
  }

  // 获取滚动条幅
  Future getBanner(String labelId) {
    return commonRepository.getBanner().then((list) {
      // sink中推入数据
      _bannerSink.add(UnmodifiableListView<BannerModel>(list));
    }).catchError((onError) {
      _bannerSink.add([]);
    });
  }

  @override
  void dispose() {
    _banner.close();
  }
}
