import 'package:dartz/dartz.dart';
import '../../domain/entities/trade_order.dart';
import '../../domain/entities/kline.dart';
import '../../domain/repositories/trading_repository.dart';
import '../../core/error/exceptions.dart';
import '../../core/error/failures.dart';
import '../datasources/remote/trading_remote_datasource.dart';

class TradingRepositoryImpl implements TradingRepository {
  final TradingRemoteDataSource remoteDataSource;

  TradingRepositoryImpl({required this.remoteDataSource});

  @override
  Future<Either<Failure, String>> getCurrentPrice() async {
    try {
      final result = await remoteDataSource.getCurrentPrice();
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }

  @override
  Future<Either<Failure, List<Kline>>> getKlineData(String period) async {
    try {
      final result = await remoteDataSource.getKlineData(period);
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }

  @override
  Future<Either<Failure, TradeOrder>> buyShares({
    required String accountSequence,
    required String amount,
  }) async {
    try {
      final result = await remoteDataSource.buyShares(
        accountSequence: accountSequence,
        amount: amount,
      );
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }

  @override
  Future<Either<Failure, TradeOrder>> sellShares({
    required String accountSequence,
    required String amount,
  }) async {
    try {
      final result = await remoteDataSource.sellShares(
        accountSequence: accountSequence,
        amount: amount,
      );
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }

  @override
  Future<Either<Failure, List<TradeOrder>>> getOrders(
    String accountSequence, {
    int page = 1,
    int limit = 20,
  }) async {
    try {
      final result = await remoteDataSource.getOrders(
        accountSequence,
        page: page,
        limit: limit,
      );
      return Right(result);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }

  @override
  Future<Either<Failure, void>> transfer({
    required String accountSequence,
    required String amount,
    required String direction,
  }) async {
    try {
      await remoteDataSource.transfer(
        accountSequence: accountSequence,
        amount: amount,
        direction: direction,
      );
      return const Right(null);
    } on ServerException catch (e) {
      return Left(ServerFailure(e.message));
    } on NetworkException {
      return Left(const NetworkFailure());
    }
  }
}
