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

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Common\Define\Controller\Json;
use ProVM\Money\Value;

class Values {
  use Json;

  public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
    $values = $factory->find(Value::class)->many();
    $output = compact('values');
    array_walk($output['values'], function(&$item) {
      $item = $item->asArray();
    });
    return $this->withJson($response, $output);
  }
  public function get(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response {
    if ($date_time !== null) {
      $value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one();
      $output = [
        'get_data' => compact('currency_id', 'date_time', 'base_id'),
        'value' => null
      ];
      if ($value) {
        $output['value'] = $value->asArray();
      }
    } else {
      $values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many();
      $output = [
        'get_data' => compact('currency_id', 'date_time', 'base_id'),
        'values' => null
      ];
      if ($values) {
        $vals = [];
        foreach ($values as $value) {
          $vals []= $value->asArray();
        }
        $output['values'] = $vals;
      }
    }
    return $this->withJson($response, $output);
  }
  public function add(Request $request, Response $response, ModelFactory $factory): Response {
    $post = json_decode($request->getBody()->getContents());
    $values = [];
    if (is_array($post)) {
      foreach ($post as $obj) {
        if (!is_object($obj)) {
          continue;
        }
        $values []= Value::add($factory, $obj);
      }
    } else {
      $values []= Value::add($factory, $post);
    }
    $output = [
      'post_data' => $post,
      'values' => $values
    ];
    return $this->withJson($response, $output);
  }
  public function edit(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response {
    $post = json_decode($request->getBody()->getContents());
    $output = [
      'get_data' => compact('currency_id', 'date_time', 'base_id'),
      'post_data' => $post
    ];
    if ($date_time !== null) {
      $value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one();
      $edited = false;
      if ($value) {
        $edited = $value->edit($post);
        $output['value'] = $value->asArray();
        $output['edited'] = $edited;
      }
    } else {
      $values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many();
      $vals = [];
      foreach ($values as $value) {
        $edited = $value->edit($post);
        $vals []= ['value' => $value, 'edited' => $edited];
      }
      $output['values'] = $vals;
    }
    return $this->withJson($response, $output);
  }
  public function delete(Request $request, Response $response, ModelFactory $factory, $currency_id, $base_id, $date_time = null): Response {
    $output = ['get_data' => compact('currency_id', 'date_time', 'base_id'), 'value' => null, 'deleted' => false];
    if ($date_time !== null) {
      $value = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id], ['date_time', $date_time]])->one();
      if ($value) {
        $output['value'] = $value->asArray();
        $status = $value->delete();
        $output['deleted'] = $status;
      }
    } else {
      $values = $factory->find(Value::class)->where([['currency_id', $currency_id], ['base_id', $base_id]])->many();
      $vals = [];
      foreach ($values as $value) {
        $val = ['value' => $value];
        $status = $value->delete();
        $val['deleted'] = $status;
        $vals []= $val;
      }
      $output['values'] = $vals;
    }
    return $this->withJson($response, $output);
  }
}
