import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:zhiying_base_widget/pages/message_notice_page/bloc/message_notice_repository.dart';
import 'package:zhiying_base_widget/pages/message_notice_page/model/message_notice_data_model.dart';
import 'package:zhiying_base_widget/pages/message_notice_page/model/message_notice_style_model.dart';
import 'package:zhiying_comm/util/empty_util.dart';
import 'package:zhiying_comm/util/log/let_log.dart';

part 'message_notice_event.dart';

part 'message_notice_state.dart';

class MessageNoticeBloc extends Bloc<MessageNoticeEvent, MessageNoticeState> {
  // MessageNoticeBloc() : super(MessageNoticeInitial());

  MessageNoticeRepository repository;

  MessageNoticeBloc(this.repository);

  @override
  MessageNoticeState get initialState => MessageNoticeInitial();

  @override
  Stream<MessageNoticeState> mapEventToState(
    MessageNoticeEvent event,
  ) async* {
    /// 初始化
    if (event is MessageNoticeInitEvent) {
      yield* _mapInitEventToState(event);
    }

    /// 消除红点
    if (event is MessageNoticeClearPointEvent) {
      yield* _mapClearPointEventToState(event);
    }

    /// 下拉刷新
    if (event is MessageNoticeOnRefreshEvent) {
      yield* _mapOnRefreshEventToState(event);
    }

    /// 上拉更多
    if (event is MessageNoticeOnLoadEvent) {
      yield* _mapOnLoadEventToState(event);
    }
  }

  /// 初始化
  Stream<MessageNoticeState> _mapInitEventToState(MessageNoticeInitEvent event) async* {
    var cacheStyle = await repository.fetchCachedStyle();
    if (!EmptyUtil.isEmpty(cacheStyle)) {
      // yield MessageNoticeLoadedState(styleModel: cacheStyle);
    }
    var netStyle = await repository.fetchNetStyle();
    if (!EmptyUtil.isEmpty(netStyle)) {
      // yield MessageNoticeLoadedState(styleModel: netStyle);
      Logger.log(netStyle.toJson().toString());
    }
    var data = await repository.fetchInitData();
    if (!EmptyUtil.isEmpty(data) && (!EmptyUtil.isEmpty(netStyle) || !EmptyUtil.isEmpty(cacheStyle))) {
      yield MessageNoticeLoadedState(dataModel: data, styleModel: !EmptyUtil.isEmpty(netStyle) ? netStyle : cacheStyle);
    } else {
      yield MessageNoticeInitErrorState();
    }
  }

  /// 消除消息右上角红点
  Stream<MessageNoticeState> _mapClearPointEventToState(MessageNoticeClearPointEvent event) async* {
    var style = repository.getStyleModel();
    var data = await repository.fetchClearPointData(event.selectId);
    if (!EmptyUtil.isEmpty(data)) {
      yield MessageNoticeLoadedState(styleModel: style, dataModel: data);
    }else{
      yield MessageNoticeOnRefreshErrorState();
    }
  }

  /// 下拉刷新
  Stream<MessageNoticeState> _mapOnRefreshEventToState(MessageNoticeOnRefreshEvent event) async* {
    var style = repository.getStyleModel();
    var data = await repository.fetchRefreshData();
    if (!EmptyUtil.isEmpty(data)) {
      yield MessageNoticeOnRefreshSuccessState();
      yield MessageNoticeLoadedState(styleModel: style, dataModel: data);
    } else {
      yield MessageNoticeOnRefreshErrorState();
    }
  }

  /// 上拉更多
  Stream<MessageNoticeState> _mapOnLoadEventToState(MessageNoticeOnLoadEvent event) async* {
    var style = repository.getStyleModel();
    var data = await repository.fetchLoadData();
    if (!EmptyUtil.isEmpty(data)) {
      yield MessageNoticeOnLoadSuccessState();
      yield MessageNoticeLoadedState(styleModel: style, dataModel: data);
    } else {
      yield MessageNoticeOnLoadErrorState();
    }
  }
}
