import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
import 'package:zhiying_base_widget/widgets/favorites/goods_list/model/favorites_goods_list_model.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_model.dart';
import 'package:zhiying_comm/util/empty_util.dart';

import 'favorites_goods_list_repository.dart';

part 'favorites_goods_list_event.dart';

part 'favorites_goods_list_state.dart';

class FavoritesGoodsListBloc extends Bloc<FavoritesGoodsListEvent, FavoritesGoodsListState> {
  @override
  FavoritesGoodsListState get initialState => FavoritesGoodsListInitial();

  FavoritesGoodsListRepository repository;

  FavoritesGoodsListBloc(this.repository);

  @override
  Stream<FavoritesGoodsListState> mapEventToState(
    FavoritesGoodsListEvent event,
  ) async* {
    /// 初始化数据
    if (event is FavoritesGoodsListInitEvent) {
      yield* _mapInitEventToState(event);
    }

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

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

    /// 删除
    if (event is FavoritesGoodsListDeleteEvent) {
      yield* _mapDeleteEventToState(event);
    }

    /// 搜索
    if (event is FavoritesGoodsListSearchEvent) {
      yield* _mapSearchEventToState(event);
    }

    /// 单个商品选中
    if(event is FavoritesGoodsListSignGoodsSelectEvent){
      yield* _mapSelectSignEventToState(event);
    }

    /// 所有商品选中
    if(event is FavoritesGoodsListGoodsSelectAllEvent){
      yield* _mapSelectAllEventToState(event);
    }

    /// 取消选中所有商品
    if(event is FavoritesGoodsListGoodsCancelAllEvent){
      yield* _mapCancelAllEventToState(event);
    }

    /// 右滑删除单个商品
    if(event is FavoritesGoodsListSlideDeleteEvent){
      yield* _mapSlideDeleteEventToState(event);
    }

    // /// 切换样式
    // if (event is FavoritesGoodsListChangeStyleEvent) {
    //   yield* _mapChangeStyleEventToState(event);
    // }
  }

  /// 初始化数据
  Stream<FavoritesGoodsListState> _mapInitEventToState(FavoritesGoodsListInitEvent event) async* {
    var result = await repository.fetchInit();
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListInitErrorState();
    }
  }

  /// 下拉刷新
  Stream<FavoritesGoodsListState> _mapOnRefreshEventToState(FavoritesGoodsListOnRefreshEvent event) async* {
    var result = await repository.fetchRefresh();
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListRefreshSuccessState();
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListRefreshErrorState();
      yield FavoritesGoodsListErrorState();
    }
  }

  /// 上拉更多
  Stream<FavoritesGoodsListState> _mapOnLoadEventToState(FavoritesGoodsListOnLoadEvent event) async* {
    var result = await repository.fetchLoad();
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListLoadSuccessState();
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListLoadErrorState();
      yield FavoritesGoodsListErrorState();
    }
  }

  /// 删除
  Stream<FavoritesGoodsListState> _mapDeleteEventToState(FavoritesGoodsListDeleteEvent event) async* {
    int length = repository.getSelectLength();
    if(length > 0) {
      var result = await repository.delete();
      if (!EmptyUtil.isEmpty(result)) {
        yield FavoritesGoodsListLoadedState(dataModel: result);
      } else {
        yield FavoritesGoodsListErrorState();
      }
    }else{
      yield FavoritesGoodsListNotSelectState();
    }
  }

  /// 搜索
  Stream<FavoritesGoodsListState> _mapSearchEventToState(FavoritesGoodsListSearchEvent event) async* {
    var result = await repository.search(event.keyword);
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListInitErrorState();
    }
  }

  /// 所有商品选中
  Stream<FavoritesGoodsListState> _mapSelectAllEventToState(FavoritesGoodsListGoodsSelectAllEvent event) async*{
    var result = await repository.selectAllData();
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListErrorState();
    }
  }

  /// 商品的全部取消
  Stream<FavoritesGoodsListState> _mapCancelAllEventToState(FavoritesGoodsListGoodsCancelAllEvent event) async*{
    var result = await repository.cancelSelectAllData();
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListErrorState();
    }
  }

  /// 单个商品选中
  Stream<FavoritesGoodsListState> _mapSelectSignEventToState(FavoritesGoodsListSignGoodsSelectEvent event) async*{
    var result = await repository.signSelectData(event.index);
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListErrorState();
    }
  }

  /// 右滑动删除商品
  Stream<FavoritesGoodsListState> _mapSlideDeleteEventToState(FavoritesGoodsListSlideDeleteEvent event) async*{
    var result = await repository.slideDeleteData(event.index);
    if (!EmptyUtil.isEmpty(result)) {
      yield FavoritesGoodsListLoadedState(dataModel: result);
    } else {
      yield FavoritesGoodsListErrorState();
    }
  }


  // /// 切换样式
  // Stream<FavoritesGoodsListState> _mapChangeStyleEventToState(FavoritesGoodsListChangeStyleEvent event) async* {
  //   var result = await repository.fetchInit();
  //   if (!EmptyUtil.isEmpty(result)) {
  //     yield FavoritesGoodsListLoadedState(dataModel: result);
  //   } else {
  //     yield FavoritesGoodsListInitErrorState();
  //   }
  // }
}
