import 'dart:async';

import 'package:boilerplate/data/models/product/get_product_list_req_model.dart';
import 'package:boilerplate/domain/entities/categories/categories_data.dart';
import 'package:boilerplate/domain/entities/master/banner_data.dart';
import 'package:boilerplate/domain/usecases/banner/get_banner_use_case.dart';
import 'package:boilerplate/domain/usecases/categories/get_categories_use_case.dart';
import 'package:boilerplate/domain/usecases/categories/get_intellectuall_type_use_case.dart';
import 'package:boilerplate/domain/usecases/product/get_product_list_use_case.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../domain/entities/categories/intellectual_type_data.dart';
import '../../../domain/entities/product/product_data.dart';
import '../../../domain/usecases/product/get_new_product_use_case.dart';

part 'home_event.dart';
part 'home_state.dart';

@injectable
class HomeBloc extends Bloc<HomeEvent, HomeState> {
  final GetIntellectualTypesUseCase _getIntellectualTypesUseCase;
  final GetCategoriesUseCase _getCategoriesUseCase;

  final GetNewProductListUseCase _getNewProductListUseCase;
  final GetBannerUseCase _getBannerUseCase;

  HomeBloc(
    this._getIntellectualTypesUseCase,
    this._getCategoriesUseCase,
    this._getNewProductListUseCase,
    this._getBannerUseCase,
  ) : super(LoadingState(HomeStateData())) {
    on<GetHomeDataEvent>(_onGetHomeData);
  }

  Future<void> _onGetHomeData(
    GetHomeDataEvent event,
    Emitter<HomeState> emit,
  ) async {
    emit(LoadingState(state.data));

    await Future.wait([
      _getBanners(emit),
      _getIntellectualType(emit),
      _getCategories(emit),
      _getProductList(emit),
    ]);
  }

  Future<void> _getBanners(Emitter<HomeState> emit) async {
    final res = await _getBannerUseCase.execute();

    return res.fold((data) {
      emit(GetDataSuccessState(state.data.copyWith(
        newBanners: data,
      )));
    }, (fail) => null);
  }

  Future<void> _getIntellectualType(Emitter<HomeState> emit) async {
    final res = await _getIntellectualTypesUseCase.execute();

    return res.fold((data) {
      emit(GetDataSuccessState(state.data.copyWith(
        newIntellectualTypes: data,
      )));
    }, (fail) => null);
  }

  Future<void> _getCategories(Emitter<HomeState> emit) async {
    final res = await _getCategoriesUseCase.execute();

    res.fold((data) {
      emit(GetDataSuccessState(state.data.copyWith(
        newCategories: data,
      )));
    }, (fail) => null);
  }

  // Future<void> _getProductNewIdeaList(Emitter<HomeState> emit) async {
  //   final res = await _getProductListUseCase.execute(
  //       GetProductListReqModel(sort: 'desc', limit: 10, type: 'product_new'));
  //
  //   res.fold((data) {
  //     emit(GetDataSuccessState(state.data.copyWith(
  //       newProductNewIdeas: data,
  //     )));
  //   }, (fail) => null);
  // }

  Future<void> _getProductList(Emitter<HomeState> emit) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    final res = await _getNewProductListUseCase.execute(
        GetProductListReqModel(lang: prefs.getString('lang').toString()));
    res.fold((data) {
      emit(GetDataSuccessState(state.data.copyWith(
        newProductNewIdeas: data,
      )));
    }, (fail) => null);
  }
}
