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/list_category_response.dart';
import 'package:e_commerce_training/src/model/response/list_choose_category.dart';
import 'package:e_commerce_training/src/model/response/list_community_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:easy_localization/src/public_ext.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'choose_category.dart';

class ChooseCategoryCubit extends Cubit<ChooseCategoryState> {
  final AppRepository repository;
  List<ChooseCategoryData> listCategory = [];
  List<ChooseCategoryData> listSelectedCategory = [];
  List<CommunityData> listCommunity = [];
  List<CommunityData> listSelectedCommunity = [];

  ChooseCategoryCubit(this.repository) : super(InitialChooseCategoryState());

  void getListCategory({bool isRefresh = false}) async {
    emit(isRefresh ? InitialChooseCategoryState() : ChooseCategoryLoading());
    ApiResult<List<ChooseCategoryData>> listCategoryTask = await repository.getChooseCategory();
    listCategoryTask.when(success: (List<ChooseCategoryData> data) async {
      listCategory = data;
      emit(GetListCategorySuccess());
    }, failure: (NetworkExceptions error) async {
      emit(ChooseCategoryFailure(NetworkExceptions.getErrorMessage(error)));
      try {
        ChooseCategoryResponse listCategoryTask =
            ChooseCategoryResponse.fromJson(await Util.parseJson("list_choose_category.json"));
        listCategory = listCategoryTask.Data ?? [];
        listSelectedCategory = [listCategory.first];
        emit(GetListCategorySuccess());
      } catch (e) {
        logger.e(e);
        emit(ChooseCategoryFailure(R.string.error.tr()));
      }
    });
  }

  void getListCommunity({bool isRefresh = false}) async {
    emit(isRefresh ? InitialChooseCategoryState() : ChooseCategoryLoading());
    ApiResult<List<CommunityData>> listCommunityTask = await repository.getCommunity();
    listCommunityTask.when(success: (List<CommunityData> data) async {
      listCommunity = data;
      emit(GetListCommunitySuccess());
    }, failure: (NetworkExceptions error) async {
      emit(ChooseCategoryFailure(NetworkExceptions.getErrorMessage(error)));
      try {
        ListCommunityResponse listCommunityTask =
            ListCommunityResponse.fromJson(await Util.parseJson("list_community.json"));
        listCommunity = listCommunityTask.data ?? [];
        listSelectedCommunity = [listCommunity.first];
        emit(GetListCommunitySuccess());
      } catch (e) {
        logger.e(e);
        emit(ChooseCategoryFailure(R.string.error.tr()));
      }
    });
  }

  void selectCategory(ChooseCategoryData selectedCategory) {
    emit(ChooseCategoryLoading());
    int index = listSelectedCategory.indexWhere((element) => element.id == selectedCategory.id);
    if (index >= 0) {
      listSelectedCategory.removeAt(index);
    } else {
      listSelectedCategory.add(selectedCategory);
    }
    emit(ChooseCategorySuccess());
  }

  void selectCommunity(CommunityData selectedCommunity) {
    emit(ChooseCategoryLoading());
    int index = listSelectedCommunity.indexWhere((element) => element.id == selectedCommunity.id);
    if (index >= 0) {
      listSelectedCommunity.removeAt(index);
    } else {
      listSelectedCommunity.add(selectedCommunity);
    }
    emit(ChooseCommunitySuccess());
  }
}
