import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:zhiying_comm/util/global_config.dart';
import './bloc.dart';
import 'login_repository.dart';
import 'package:zhiying_comm/util/empty_util.dart';

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  LoginRepository repository;

  LoginBloc({this.repository});

  @override
  LoginState get initialState => InitialLoginState();

  @override
  Stream<LoginState> mapEventToState(
    LoginEvent event,
  ) async* {
    /// 初始化
    if (event is LoginInitEvent) {
      yield* _mapLoginInitEventToState(event);
    }

    /// 淘宝登录
    if (event is LoginThirdAliEvent) {
      yield* _mapLoginAliEventToState(event);
    }

    /// 苹果登录
    if (event is LoginThirdAppleEvent) {
      yield* _mapLoginAppleEventToState(event);
    }

    /// QQ登录
    if (event is LoginThirdQQEvent) {
      yield* _mapLoginQQEventToState(event);
    }

    /// 微信登录
    if (event is LoginThirdWeChatEvent) {
      yield* _mapLoginWeChatEventToState(event);
    }
  }

  /// 获取页面数据
  Stream<LoginState> _mapLoginInitEventToState(LoginInitEvent event) async* {
    var cache = await repository.fetchCachePageData();
    if (!EmptyUtil.isEmpty(cache)) yield LoginCacheState(model: cache);
    var result = await repository.fetchNetPageData();
    if (!EmptyUtil.isEmpty(result))
      yield LoginLoadedState(model: result);
    else
      yield LoginErrorState();
  }

  /// 淘宝登录
  Stream<LoginState> _mapLoginAliEventToState(LoginThirdAliEvent event) async* {
    var result = await repository.aliLogin(
      nick: event.nick,
      avatarUrl: event.avatarUrl,
      openId: event.openId,
      openSid: event.openSid,
      topAccessToken: event.topAccessToken,
      topAuthCode: event.topAuthCode,
    );

    if(!EmptyUtil.isEmpty(result)){
      yield LoginThirdLoginSuccessState(model: result, thirdType: GlobalConfig.LOGIN_THIRD_ALI);
    }else{
      yield LoginThirdLoginErrorState();
    }
  }

  /// 微信登录
  Stream<LoginState> _mapLoginWeChatEventToState(LoginThirdWeChatEvent event) async* {
    var result = await repository.weChatLogin(event?.model);
    if(!EmptyUtil.isEmpty(result)){
      yield LoginThirdLoginSuccessState(model: result, thirdType: GlobalConfig.LOGIN_THIRD_WECHAT);
    }else{
      yield LoginThirdLoginErrorState();
    }
  }

  /// QQ 登录
  Stream<LoginState> _mapLoginQQEventToState(LoginThirdQQEvent event) async* {
    var result = await repository.qqLogin(event?.model);
    if(!EmptyUtil.isEmpty(result)){
      yield LoginThirdLoginSuccessState(model: result, thirdType: GlobalConfig.LOGIN_THIRD_QQ);
    }else{
      yield LoginThirdLoginErrorState();
    }
  }

  /// 苹果登录
  Stream<LoginState> _mapLoginAppleEventToState(LoginThirdAppleEvent event) async* {
    var result = await repository.appleLogin(identityToken: event.identityToken);
    if(!EmptyUtil.isEmpty(result)){
      yield LoginThirdLoginSuccessState(model: result, thirdType: GlobalConfig.LOGIN_THIRD_APPLE);
    }else{
      yield LoginThirdLoginErrorState();
    }
  }
}
