import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:farm_tpf/custom_model/NotificationDTO.dart';
import 'package:farm_tpf/custom_model/NotificationObjectDTO.dart';
import 'package:farm_tpf/custom_model/UpdateNoti.dart';
import 'package:farm_tpf/data/api/app_exception.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/utils/const_string.dart';
import 'package:meta/meta.dart';

part 'noti_event.dart';
part 'noti_state.dart';

class NotiBloc extends Bloc<NotiEvent, NotiState> {
  final Repository repository;
  NotiBloc({required this.repository}) : super(NotiInitial());
  static int pageSize = 20;

  @override
  Stream<NotiState> mapEventToState(
    NotiEvent event,
  ) async* {
    if (event is DataFetched && !(state is NotiSuccess && ((state as NotiSuccess).hasReachedMax ?? false))) {
      try {
        if (state is NotiInitial) {
          yield NotiLoadding();
          final response = await repository.getNotifications(page: 0, size: pageSize);
          List<NotificationDTO> items = <NotificationDTO>[];
          print(response.notificationDTO);
          response.notificationDTO?.forEach((e) {
            print(e);
            items.add(NotificationDTO.clone(e));
          });
          yield NotiSuccess(
              unread: response.numberUnreadTotal ?? 0,
              read: response.numberReadTotal ?? 0,
              items: items,
              page: 0,
              hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
        }
        if (state is NotiSuccess) {
          final currentState = state as NotiSuccess;
          int page = (currentState.page ?? 0) + 1;
          final response = await repository.getNotifications(page: page, size: pageSize);
          yield response.notificationDTO!.isEmpty
              ? currentState.copyWith(hasReachedMax: true)
              : NotiSuccess(
                  unread: response.numberUnreadTotal ?? 0,
                  read: response.numberReadTotal ?? 0,
                  items: currentState.items ?? [] + response.notificationDTO!,
                  page: (currentState.page ?? 0) + 1,
                  hasReachedMax: false);
        }
      } catch (e) {
        yield NotiFailure(errorString: AppException.handleError(e));
      }
    }
    if (event is OnRefresh) {
      yield NotiLoadding();
      try {
        final response = await repository.getNotifications(page: 0, size: pageSize);
        List<NotificationDTO> items = <NotificationDTO>[];
        response.notificationDTO?.forEach((e) => items.add(NotificationDTO.clone(e)));
        yield NotiSuccess(
            unread: response.numberUnreadTotal ?? 0,
            read: response.numberReadTotal ?? 0,
            items: items,
            page: 0,
            hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
      } catch (e) {
        yield NotiFailure(errorString: AppException.handleError(e));
      }
    } else if (event is OnRefreshFromNotification) {
      try {
        final response = await repository.getNotifications(page: 0, size: pageSize);
        List<NotificationDTO> items = <NotificationDTO>[];
        response.notificationDTO?.forEach((e) => items.add(NotificationDTO.clone(e)));
        yield NotiSuccess(
            unread: response.numberUnreadTotal ?? 0,
            read: response.numberReadTotal ?? 0,
            items: items,
            page: 0,
            hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
      } catch (e) {
        yield NotiFailure(errorString: AppException.handleError(e));
      }
    }

    if (event is OnUpdate) {
      yield NotiLoadding();
      try {
        //Change status notification if mark read
        if (event.currentItemId != null) {
          var updateNoti = UpdateNoti()
            ..id = event.currentItemId
            ..isRead = 1;
          await repository.updateNoti(updateNoti);
        }

        yield NotiSuccess(
            unread: event.unread, read: event.read, items: event.currentItems, page: event.currentPage, hasReachedMax: event.hasReachedMax);
      } catch (e) {
        yield NotiFailure(errorString: exception_common);
      }
    } else if (event is MarkAllNotificationUpdate) {
      yield NotiLoadding();
      try {
        await repository.updateAllNotification(event.status);
        final response = await repository.getNotifications(page: 0, size: pageSize);
        List<NotificationDTO> items = <NotificationDTO>[];
        response.notificationDTO?.forEach((e) => items.add(NotificationDTO.clone(e)));
        yield NotiSuccess(
            unread: response.numberUnreadTotal ?? 0,
            read: response.numberReadTotal ?? 0,
            items: items,
            page: 0,
            hasReachedMax: (response.notificationDTO?.length ?? 0) < pageSize ? true : false);
      } catch (e) {
        yield NotiFailure(errorString: AppException.handleError(e));
      }
    } else if (event is ReceiveDataFromSocket) {
      List<NotificationDTO> updatedItems = <NotificationDTO>[];
      event.updatedItemObject?.notificationDTO?.forEach((e) {
        updatedItems.add(NotificationDTO.clone(e));
      });

      event.currentItems.forEach((e) {
        updatedItems.add(NotificationDTO.clone(e));
      });
      yield NotiSuccess(
          unread: event.updatedItemObject?.numberUnreadTotal ?? 0,
          read: event.updatedItemObject?.numberReadTotal ?? 0,
          items: updatedItems,
          page: event.page ?? 1,
          hasReachedMax: updatedItems.length < pageSize ? true : false);
    }
  }
}
