<?php
namespace ProVM\Money\Common\Listener;

use ProVM\Common\Define\Event\Request;
use ProVM\Common\Define\Event\Response;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Money\Currency;

class Currencies {
  public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
    $currencies = $factory->find(Currency::class)->many();
    array_walk($currencies, function(&$item) {
      $item = $item->asArray();
    });
    $response->getBody()->write(compact('currencies'));
    return $response;
  }
  public function get(Request $request, Response $response, ModelFactory $factory): Response {
    $currency_id = $request->getBody()->read()['currency_id'];
    $currency = $factory->find(Currency::class)->one($currency_id);
    $response->getBody()->write(['currency' => $currency->asArray()]);
    return $response;
  }
  public function latest(Request $request, Response $response, ModelFactory $factory): Response {
    $currency_id = $request->getBody()->read()['currency_id'];
    $currency = $factory->find(Currency::class)->one($currency_id);
    $output = [
      'currency' => null,
      'value' => null
    ];
    if ($currency) {
      $output['currency'] = $currency->asArray();
      if ($currency->latest()) {
        $output['value'] = $currency->latest()->asArray();
      }
    }
    $response->getBody()->write($output);
    return $response;
  }
  public function getAliases(Request $request, Response $response, ModelFactory $factory): Response {
    $currency_id = $request->getBody()->read()['currency_id'];
    $currency = $factory->find(Currency::class)->one($currency_id);
    $output = [
      'currency' => null,
      'aliases' => []
    ];
    if ($currency) {
      $output['currency'] = $currency->asArray();
      if ($currency->aliases()) {
        $output['aliases'] = array_map(function($item) {
          return $item->asArray();
        }, $currency->aliases());
      }
    }
    $response->getBody()->write($output);
    return $response;
  }
  public function getSources(Request $request, Response $response, ModelFactory $factory): Response {
    $currency_id = $request->getBody()->read()['currency_id'];
    $currency = $factory->find(Currency::class)->one($currency_id);
    $output = [
      'currency' => null,
      'sources' => []
    ];
    if ($currency) {
      $output['currency'] = $currency->asArray();
      if ($currency->sources()) {
        $output['sources'] = array_map(function($item) {
          return $item->asArray();
        }, $currency->sources());
      }
    }
    $response->getBody()->write($output);
    return $response;
  }
  public function getValues(Request $request, Response $response, ModelFactory $factory): Response {
    $currency_id = $request->getBody()->read()['currency_id'];
    $currency = $factory->find(Currency::class)->one($currency_id);
    $output = [
      'currency' => null,
      'values' => []
    ];
    if ($currency) {
      $output['currency'] = $currency->asArray();
      if ($currency->values()) {
        $output['values'] = array_map(function($item) {
          return $item->asArray();
        }, $currency->values());
      }
    }
    $response->getBody()->write($output);
    return $response;
  }
}
