// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_crud_bloc/src/domain/data_sources/crud_data_source.dart';
import 'package:wyatt_crud_bloc/src/domain/entities/object_model.dart';
import 'package:wyatt_crud_bloc/src/domain/entities/query.dart';
import 'package:wyatt_crud_bloc/src/domain/repositories/crud_repository.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';

class CrudRepositoryImpl<Model extends ObjectModel>
    extends CrudRepository<Model> {

  CrudRepositoryImpl({
    required CrudDataSource<Model> crudDataSource,
  }) : _crudDataSource = crudDataSource;
  final CrudDataSource<Model> _crudDataSource;

  @override
  FutureOrResult<void> create(Model object, {String? id}) =>
      Result.tryCatchAsync<void, AppException, AppException>(
        () async {
          await _crudDataSource.create(object, id: id);
        },
        (error) => error,
      );

  @override
  FutureOrResult<Model?> get(String id) =>
      Result.tryCatchAsync<Model?, AppException, AppException>(
        () async => _crudDataSource.get(id),
        (error) => error,
      );

  @override
  FutureOrResult<List<Model?>> getAll() =>
      Result.tryCatchAsync<List<Model?>, AppException, AppException>(
        () async => _crudDataSource.getAll(),
        (error) => error,
      );

  @override
  FutureOrResult<void> update(
    String id, {
    Model? object,
    Map<String, dynamic>? raw,
  }) =>
      Result.tryCatchAsync<void, AppException, AppException>(
        () async => _crudDataSource.update(id, object: object, raw: raw),
        (error) => error,
      );

  @override
  FutureOrResult<void> updateAll(Map<String, Object?> raw) =>
      Result.tryCatchAsync<void, AppException, AppException>(
        () async => _crudDataSource.updateAll(raw),
        (error) => error,
      );

  @override
  FutureOrResult<void> delete(String id) =>
      Result.tryCatchAsync<void, AppException, AppException>(
        () async => _crudDataSource.delete(id),
        (error) => error,
      );

  @override
  FutureOrResult<void> deleteAll() =>
      Result.tryCatchAsync<void, AppException, AppException>(
        () async => _crudDataSource.deleteAll(),
        (error) => error,
      );

  @override
  FutureOrResult<List<Model?>> query(List<QueryInterface> conditions) =>
      Result.tryCatchAsync<List<Model?>, AppException, AppException>(
        () async => _crudDataSource.query(conditions),
        (error) => error,
      );

  @override
  StreamResult<List<Model?>> stream({
    String? id,
    List<QueryInterface>? conditions,
  }) =>
      _crudDataSource.stream(id: id, conditions: conditions).map((lst) {
        if (lst.isNotNull) {
          return Ok<List<Model?>, AppException>(lst);
        }
        return Err<List<Model?>, AppException>(ServerException());
      });
}
