import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/preference/app_preference.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/model/response/list_cart.dart';
import 'package:e_commerce_training/src/model/response/list_community_response.dart';
import 'package:e_commerce_training/src/model/response/tag_home.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/page/home_favorite/home_favorite_state.dart';
import 'package:e_commerce_training/src/util/const.dart';
import 'package:e_commerce_training/src/util/logger.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class HomeFavoriteCubit extends Cubit<HomeFavoriteState> {
  final AppRepository repository;
  List<TagHomeData> listTagHome = [];
  List<ListCartData> listCart = [];
  List<CommunityData> listCommunity = [];
  int isSelected = 0;
  bool like = false;
  bool bookMark = false;

  String get token => appPreference.getData(Const.TOKEN) ?? "";

  bool get isLoggedIn => !Util.isEmpty(token);

  HomeFavoriteCubit(this.repository) : super(InitialHomeFavoriteState());

  void refreshHomeEvent() {
    emit(HomeFavoriteLoading());
    emit(InitialHomeFavoriteState());
  }

  void getListTagHome() async {
    emit(HomeFavoriteLoading());
    ApiResult<List<TagHomeData>> tagHome = await repository.getTagHome();
    tagHome.when(success: (List<TagHomeData> data) async {
      listTagHome = data;
      emit(HomeFavoriteSuccess());
    }, failure: (NetworkExceptions error) async {
      emit(HomeFavoriteFailure(NetworkExceptions.getErrorMessage(error)));
      try {
        TagHomeResponse tagHomeResponse = TagHomeResponse.fromJson(await Util.parseJson("tag_home.json"));
        listTagHome = tagHomeResponse.Data ?? [];
        emit(HomeFavoriteSuccess());
      } catch (e) {
        logger.e(e);
        emit(HomeFavoriteFailure(R.string.error.tr()));
      }
    });
  }

  void getListCart({bool isRefresh = false, bool isLoadMore = false}) async {
    emit(isRefresh && isLoadMore ? HomeFavoriteLoading() : InitialHomeFavoriteState());
    ApiResult<List<ListCartData>> listCartHome = await repository.getListCart();
    listCartHome.when(success: (List<ListCartData> data) async {
      listCart = data;
      emit(HomeFavoriteSuccess());
    }, failure: (NetworkExceptions error) async {
      emit(HomeFavoriteFailure(NetworkExceptions.getErrorMessage(error)));
      try {
        ListCartResponse listCartResponse = ListCartResponse.fromJson(await Util.parseJson("list_cart.json"));
        listCart = listCartResponse.Data ?? [];
        emit(HomeFavoriteSuccess());
      } catch (e) {
        logger.e(e);
        emit(HomeFavoriteFailure(R.string.error.tr()));
      }
    });
  }
  void getListCommunity({bool isRefresh = false}) async {
    emit(isRefresh ? InitialHomeFavoriteState() : HomeFavoriteLoading());
    ApiResult<List<CommunityData>> listCommunityTask = await repository.getCommunity();
    listCommunityTask.when(success: (List<CommunityData> data) async {
      listCommunity = data;
      emit(HomeFavoriteSuccess());
    }, failure: (NetworkExceptions error) async {
      emit(HomeFavoriteFailure(NetworkExceptions.getErrorMessage(error)));
      try {
        ListCommunityResponse listCommunityTask =
        ListCommunityResponse.fromJson(await Util.parseJson("list_community.json"));
        listCommunity = listCommunityTask.data ?? [];
        emit(HomeFavoriteSuccess());
      } catch (e) {
        logger.e(e);
        emit(HomeFavoriteFailure(R.string.error.tr()));
      }
    });
  }


  void checkSelected(int index) {
    emit(HomeFavoriteLoading());
    this.isSelected = index;
    emit(HomeFavoriteSuccess());
  }

  void checkLike() {
    emit(HomeFavoriteLoading());
    this.like = !like;
    emit(HomeFavoriteSuccess());
  }

  void checkBookMark() {
    emit(HomeFavoriteLoading());
    this.bookMark = !bookMark;
    emit(HomeFavoriteSuccess());
  }
}
