// Copyright (C) 2023 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/wyatt_http_client.dart';

Future<void> testSimpleGet() async {
  print('testSimpleGet');
  final pipeline = Pipeline()
    ..addMiddleware(const BodyToJsonMiddleware())
    ..addMiddleware(const SimpleLoggerMiddleware());

  final client = MiddlewareClient(pipeline: pipeline);

  final response = await client
      .get(Uri.parse('https://jsonplaceholder.typicode.com/todos/1'));
  print(response.body);
}

Future<void> testUriPrefix() async {
  print('testUriPrefix');
  final pipeline = Pipeline()
    ..addMiddleware(
      const UriPrefixMiddleware(
        protocol: Protocols.https,
        authority: 'jsonplaceholder.typicode.com',
      ),
    )
    ..addMiddleware(const BodyToJsonMiddleware())
    ..addMiddleware(const SimpleLoggerMiddleware());

  final client = MiddlewareClient(pipeline: pipeline);

  final response = await client.get(Uri.parse('/todos/1'));
  print(response.body);
}

Future<void> testBasicAuth() async {
  print('testBasicAuth');
  final pipeline = Pipeline()
    ..addMiddleware(
      const BasicAuthMiddleware(
        username: 'guest',
        password: 'guest',
      ),
    )
    ..addMiddleware(const BodyToJsonMiddleware())
    ..addMiddleware(const SimpleLoggerMiddleware());

  final client = MiddlewareClient(pipeline: pipeline);

  final response =
      await client.get(Uri.parse('https://jigsaw.w3.org/HTTP/Basic/'));

  if (HttpStatus.from(response.statusCode).isSuccess()) {
    print("🎉 You're in!");
  } else {
    print("⭕️ Nope, you're not in.");
  }
}

void main(List<String> args) {
  testSimpleGet();
  testUriPrefix();
  testBasicAuth();
}
