// 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_http_client/src/middleware.dart';
import 'package:wyatt_http_client/src/models/middleware_context.dart';
import 'package:wyatt_http_client/src/models/middleware_request.dart';
import 'package:wyatt_http_client/src/models/middleware_response.dart';

class Pipeline {

  Pipeline() : _middlewares = <Middleware>[];
  Pipeline.fromIterable(Iterable<Middleware> middlewares)
      : _middlewares = middlewares.toList();
  final List<Middleware> _middlewares;

  int get length => _middlewares.length;

  /// Add a [Middleware] to this [Pipeline]
  Pipeline addMiddleware(Middleware middleware) {
    _middlewares.add(middleware);
    return this;
  }

  /// Create new [Pipeline] from the start or end to a specified [Middleware].
  Pipeline sub(
    Middleware middleware, {
    bool include = false,
    bool fromEnd = false,
  }) {
    final nodes = <Middleware>[];
    final list = fromEnd ? _middlewares.reversed : _middlewares;
    for (final m in list) {
      if (m != middleware) {
        nodes.add(m);
      }
      if (m == middleware) {
        if (include) {
          nodes.add(m);
        }
        break;
      }
    }
    return Pipeline.fromIterable(fromEnd ? nodes.reversed : nodes);
  }

  Future<MiddlewareRequest> onRequest(
    MiddlewareContext context,
    MiddlewareRequest request,
  ) async {
    print('\n\nNEW REQUEST\n');
    MiddlewareRequest req = request..apply();
    MiddlewareContext ctx = context.copyWith(lastRequest: req);
    for (final middleware in _middlewares) {
      if (middleware is OnRequestMiddleware) {
        req = await (middleware as OnRequestMiddleware)
            .onRequest(ctx, request);
        ctx = context.copyWith(lastRequest: req);
      }
    }
    return req;
  }

  Future<MiddlewareResponse> onResponse(
    MiddlewareContext context,
    MiddlewareResponse response,
  ) async {
    print('\n\nNEW RESPONSE\n');
    MiddlewareResponse res = response;
    MiddlewareContext ctx = context.copyWith(lastResponse: res);
    for (final middleware in _middlewares.reversed) {
      if (middleware is OnResponseMiddleware) {
        res = await (middleware as OnResponseMiddleware)
            .onResponse(ctx, response);
        ctx = context.copyWith(lastResponse: res);
      }
    }
    return res;
  }

  @override
  String toString() {
    final req = <String>[];
    final res = <String>[];
    for (final middleware in _middlewares) {
      if (middleware is OnRequestMiddleware) {
        req.add(middleware.getName());
      }
      if (middleware is OnResponseMiddleware) {
        res.insert(0, middleware.getName());
      }
    }
    return '[Req] -> ${req.join(' -> ')}\n[Res] -> ${res.join(' -> ')}';
  }
}
