import 'package:bloc/bloc.dart';
import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/model/response/catalog_response.dart';
import 'package:e_commerce_training/src/model/response/video_response.dart';
import 'package:e_commerce_training/src/model/service/api_result.dart';
import 'package:e_commerce_training/src/model/service/network_exceptions.dart';
import 'package:e_commerce_training/src/util/logger.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:easy_localization/easy_localization.dart';

part 'video_state.dart';

class VideoCubit extends Cubit<VideoState> {
  VideoCubit(this.repository) : super(VideoInitial());
  final AppRepository repository;
  int index = 0;
  int _currentPage = 1;
  List<List<VideoData>> listVideo = [];
  List<CatalogData> listCatalogVideo = [];
  void onChangeIndex(int index) {
    this.index = index;
    if (Util.isEmpty(listVideo[index])) getListVideo(index);
  }
  void getListVideo(int index,
      {bool isRefresh = false, bool isLoadMore = false}) async {
    emit((!isRefresh && !isLoadMore) ? VideoLoading() : VideoInitial());
    if (isRefresh) _currentPage = 1;
    if (isLoadMore) {
      _currentPage++;
    }
    ApiResult<List<VideoData>> listBlogResult =
    await repository.getListVideo(listCatalogVideo[index].catalogId ?? 0, pageNumber: _currentPage);
    listBlogResult.when(success: (List<VideoData> data) async {
      listVideo[index] = data;
      emit(VideoSuccess());
    }, failure: (NetworkExceptions error) async {
      emit(VideoFailure(NetworkExceptions.getErrorMessage(error)));
      // Fake get list blog by catalog
      try {
        VideoResponse videoResponse =
        VideoResponse.fromJson(await Util.parseJson("video.json"));
        listVideo[index] = videoResponse.data ?? [];
        emit(VideoSuccess());
      } catch (e) {
        logger.e(e);
        emit(VideoFailure(R.string.error.tr()));
      }
    });
  }
  void getCatalogVideo() async {
    emit(VideoLoading());
    bool isFirstTime = Util.isEmpty(listCatalogVideo);
    ApiResult<List<CatalogData>> catalogBlogResult =
    await repository.getCatalogVideo();
    catalogBlogResult.when(success: (List<CatalogData> data) async {
      listCatalogVideo = data;
      listVideo = List.filled(listCatalogVideo.length, []);
      emit(GetListVideoSuccess());
      if (isFirstTime) onChangeIndex(0);
    }, failure: (NetworkExceptions error) async {
      emit(VideoFailure(NetworkExceptions.getErrorMessage(error)));
      // Fake get list catalog blog
      try {
        CatalogResponse catalogResponse =
        CatalogResponse.fromJson(await Util.parseJson("catalog_blog.json"));
        listCatalogVideo = catalogResponse.data ?? [];
        listVideo = List.filled(listCatalogVideo.length, []);
        emit(GetListVideoSuccess());
        if (isFirstTime) onChangeIndex(0);
      } catch (e) {
        logger.e(e);
        emit(VideoFailure(R.string.error.tr()));
      }
    });
  }
}
