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/blog_response.dart';
import 'package:e_commerce_training/src/model/response/catalog_response.dart';
import 'package:easy_localization/easy_localization.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';

part 'blog_state.dart';

class BlogCubit extends Cubit<BlogState> {
  final AppRepository repository;

  BlogCubit(this.repository) : super(BlogInitial());

  List<List<BlogData>> listBlog = [];
  List<CatalogData> listCatalogBlog = [];
  int index = 0;
  int _currentPage = 1;
  bool hasMorePage = true;

  void onChangeIndex(int index) {
    this.index = index;
    if (Util.isEmpty(listBlog[index])) getListBlog(index);
  }

  void getListBlog(int index,
      {bool isRefresh = false, bool isLoadMore = false}) async {
    emit((!isRefresh && !isLoadMore) ? BlogLoading() : BlogInitial());
    if (isRefresh) _currentPage = 1;
    if (isLoadMore) {
      _currentPage++;
    }
    ApiResult<List<BlogData>> listBlogResult =
        await repository.getListBlog(listCatalogBlog[index].catalogId ?? 0, pageNumber: _currentPage);
    listBlogResult.when(success: (List<BlogData> data) async {
      listBlog[index] = data;
      emit(BlogSuccess());
    }, failure: (NetworkExceptions error) async {
      emit(BlogFailure(NetworkExceptions.getErrorMessage(error)));
      // Fake get list blog by catalog
      try {
        BlogResponse blogResponse =
            BlogResponse.fromJson(await Util.parseJson("blog.json"));
        listBlog[index] = blogResponse.data ?? [];
        emit(BlogSuccess());
      } catch (e) {
        logger.e(e);
        emit(BlogFailure(R.string.error.tr()));
      }
    });
  }

  void getCatalogBlog() async {
    emit(BlogLoading());
    bool isFirstTime = Util.isEmpty(listCatalogBlog);
    ApiResult<List<CatalogData>> catalogBlogResult =
        await repository.getCatalogBlog();
    catalogBlogResult.when(success: (List<CatalogData> data) async {
      listCatalogBlog = data;
      listBlog = List.filled(listCatalogBlog.length, []);
      emit(GetListCatalogSuccess());
      if (isFirstTime) onChangeIndex(0);
    }, failure: (NetworkExceptions error) async {
      emit(BlogFailure(NetworkExceptions.getErrorMessage(error)));
      // Fake get list catalog blog
      try {
        CatalogResponse catalogResponse =
            CatalogResponse.fromJson(await Util.parseJson("catalog_blog.json"));
        listCatalogBlog = catalogResponse.data ?? [];
        listBlog = List.filled(listCatalogBlog.length, []);
        emit(GetListCatalogSuccess());
        if (isFirstTime) onChangeIndex(0);
      } catch (e) {
        logger.e(e);
        emit(BlogFailure(R.string.error.tr()));
      }
    });
  }
}
