<?php

namespace App\Controllers\Seller;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use \App\Controllers\Container;

class SellerDash extends Container
{
  public function all(Request $request, Response $response, $args)
  {

    $route = $request->getAttribute('route');
    $dock = $route->getArgument('dock');
    $entityManager = $this->container->get('em');
    $ordersRepository = $entityManager->getRepository('App\Models\Entity\DataOrderSellerDb');
    $orders = $ordersRepository->findAll();
    return $this->listarPedidosPainelPrincipal($orders, $dock, $response);
  }

  public function listarPedidosPainelPrincipal($orders, $dock, $response)
  {
    foreach ($orders as $ord) {
      if ($dock == $ord->getWarehouseId()) {
        switch ($ord->getStatus()) {
          case 'invoiced':
            $statusOrders[] = $ord->getId();
            break;
          case 'ready-for-handling':
            $readyOrders[] = $ord->getId();
            break;
          case 'handling':
            $readyOrders[] = $ord->getId();
            break;
          case 'canceled':
            $canceledOrders[] = $ord->getId();
          default:
            $orderSeller[] = $ord->getId();
            break;
        }
      }
    }

    if (empty($orderSeller)) {
      throw new \Exception("Sem pedidos", 404);
    }

    $quantityOrders = count($orderSeller);

    if (!empty($readyOrders)) {
      $ready = count($readyOrders);
      $manuseio = $readyOrders;
    } else {
      $ready = 0;
    }

    if (!empty($statusOrders)) {
      $invoiced = count($statusOrders);
      $faturado = $statusOrders;
    } else {
      $invoiced = 0;
    }

    if (!empty($canceledOrders)) {
      $canceled = count($canceledOrders);
      $cancelado = $canceledOrders;
    } else {
      $canceled = 0;
    }

    $return = $response->withJson([
      'orders' => $quantityOrders,
      'invoiced' => $invoiced,
      'canceled' => $canceled,
      'ready' => $ready
    ], 200)
      ->withHeader('Content-type', 'application/json');
    return $return;
  }

  public function get(Request $request, Response $response, $args)
  {
    $route = $request->getAttribute('route');
    $dock = $route->getArgument('dock');
    $action = $route->getArgument('action');

    $entityManager = $this->container->get('em');
    $ordersRepository = $entityManager->getRepository('App\Models\Entity\DataOrderSellerDb');
    $orders = $ordersRepository->findAll();

    return $this->selecionarBotaoPainelPrincipal($action,$orders,$dock,$response,$ordersRepository);
    
  }

  public function selecionarBotaoPainelPrincipal($action,$orders,$dock,$response,$ordersRepository){

    switch ($action) {
      case 'invoiced':
        foreach ($orders as $ord) {
          if ($dock == $ord->getWarehouseId() && $action == $ord->getStatus()) {
            $orderSeller[] = $ord->getId();
          }
        }
        if (empty($orderSeller)) {
          throw new \Exception("Sem pedidos", 404);
        }

        foreach ($orderSeller as $key => $o) {
          $idOrder[] = $ordersRepository->find($o);
        }

        $return = $response->withJson($idOrder, 200)->withHeader('Content-type', 'application/json');
        return $return;
        break;

      case 'canceled':
        foreach ($orders as $ord) {
          if ($dock == $ord->getWarehouseId() && $action == $ord->getStatus()) {
            $orderSeller[] = $ord->getId();
          }
        }
        if (empty($orderSeller)) {
          throw new \Exception("Sem pedidos", 404);
        }

        foreach ($orderSeller as $key => $o) {
          $idOrder[] = $ordersRepository->find($o);
        }

        $return = $response->withJson($idOrder, 200)->withHeader('Content-type', 'application/json');
        return $return;
        break;

      case 'ready-for-handling':
        foreach ($orders as $ord) {
          if ($action == $ord->getStatus() || $ord->getStatus() == 'handling') {
            if ($dock == $ord->getWarehouseId()) {
              $orderSeller[] = $ord->getId();
            }
          }
        }
        if (empty($orderSeller)) {
          throw new \Exception("Sem pedidos", 404);
        }

        foreach ($orderSeller as $key => $o) {
          $idOrder[] = $ordersRepository->find($o);
        }

        $return = $response->withJson($idOrder, 200)->withHeader('Content-type', 'application/json');
        return $return;
        break;

      default:
        throw new \Exception("Sem pedidos", 404);
        break;
    }
      
  }
}
