<?php

namespace App\Controllers\Seller\Warehouse;

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

use \App\Controllers\Container;

class UpdateItemsSeller extends Container
{
  public function updategrid(Request $request, Response $response, $args)
  {
    // env.php define variaveis
    $token = getenv('token');
    $appKey = getenv('appkey');

    $route = $request->getAttribute('route');
    $dock = $route->getArgument('dock');
    $group = $route->getArgument('item');

    $params = $request->getParams();

    $entityManager = $this->container->get('em');
    $stockRepository = $entityManager->getRepository('App\Models\Entity\StockDb');
    $stock = $stockRepository->findAll();

    foreach ($stock as $s) {
      if ($group == $s->getCodGroup()) {
        $product[] = $s->getId();
      }
    }

    if (empty($product)) {
      throw new \Exception("Item nao cadastrado na base", 404);
    }

    $countArray = count($params);

    for ($i = 0; $i < $countArray; $i++) {

      $item = $stockRepository->find($product[$i]);
      $skuVtex = $item->getSkuVtex();
      $total = $item->getTotal();
      $cvto = $item->getOutletCvto();

      if ($cvto == 0 || $cvto == null) {
        $cvto = 0;
      }

      $newStock = $params[$i]; //grava na tabela estoque

      if ($newStock != null) {

        $novoValor = $newStock; //vai pra vtex                

        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://logistics.myvtex.com/api/logistics/pvt/inventory/skus/$skuVtex/warehouses/$dock?an=arsdesign",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => false,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "PUT",
          CURLOPT_POSTFIELDS => "{\n    \"unlimitedQuantity\": false,\n    \"dateUtcOnBalanceSystem\": null,\n    \"quantity\": {$novoValor},\n    \"timeToRefill\": 0\n}",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json; charset=utf-8",
            "Accept: application/json",
            "X-VTEX-API-AppToken: {$token}",
            "X-VTEX-API-AppKey: {$appKey}"
          ),
        ));

        $responseCurl = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
          throw new \Exception("Nao foi possivel atualizar o item {$err}", 404);
        } else {
          switch ($dock) {
            case 'outletCvto':
              if ($item->getOutletCvto() > $newStock) {
                $restante = $newStock - $item->getOutletCvto();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getOutletCvto() < $newStock) {
                $restante = $newStock - $item->getOutletCvto();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              $item->setOutletCvto($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'acreSergio':
              if ($item->getAcreSergio() > $newStock) {
                $restante = $newStock - $item->getAcreSergio();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getAcreSergio() < $newStock) {
                $restante = $newStock - $item->getAcreSergio();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              $item->setAcreSergio($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'forMen':
              if ($item->getForMen() > $newStock) {
                $restante = $newStock - $item->getForMen();
                $valorRestante = - ($restante);
                $novoValor = $item->getForMen() - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getForMen() < $newStock) {
                $restante = $newStock - $item->getForMen();
                $novoValor = $item->getForMen() + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setForMen($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'maiusculo':
              if ($item->getMaiusculo() > $newStock) {
                $restante = $newStock - $item->getMaiusculo();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getMaiusculo() < $newStock) {
                $restante = $newStock - $item->getMaiusculo();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setMaiusculo($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'life':
              if ($item->getLife() > $newStock) {
                $restante = $newStock - $item->getLife();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getLife() < $newStock) {
                $restante = $newStock - $item->getLife();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setLife($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'vestylle':
              if ($item->getVestylle() > $newStock) {
                $restante = $newStock - $item->getVestylle();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getVestylle() < $newStock) {
                $restante = $newStock - $item->getVestylle();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setVestylle($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'haut':
              if ($item->getHaut() > $newStock) {
                $restante = $newStock - $item->getHaut();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getHaut() < $newStock) {
                $restante = $newStock - $item->getHaut();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setHaut($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'dimazzo':
              if ($item->getDimazzo() > $newStock) {
                $restante = $newStock - $item->getDimazzo();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getDimazzo() < $newStock) {
                $restante = $newStock - $item->getDimazzo();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setDimazzo($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;
            case 'dimazzoArapongas':
              if ($item->getDimazzoArapongas() > $newStock) {
                $restante = $newStock - $item->getDimazzoArapongas();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getDimazzoArapongas() < $newStock) {
                $restante = $newStock - $item->getDimazzoArapongas();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setDimazzoArapongas($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;
            case 'bhPadilha':
              if ($item->getBhPadilha() > $newStock) {
                $restante = $newStock - $item->getBhPadilha();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getBhPadilha() < $newStock) {
                $restante = $newStock - $item->getBhPadilha();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setBhPadilha($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;


            case 'origem':
              if ($item->getOrigem() > $newStock) {
                $restante = $newStock - $item->getOrigem();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getOrigem() < $newStock) {
                $restante = $newStock - $item->getOrigem();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setOrigem($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'emporio':
              if ($item->getEmporio() > $newStock) {
                $restante = $newStock - $item->getEmporio();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getEmporio() < $newStock) {
                $restante = $newStock - $item->getEmporio();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setEmporio($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'abusiva':
              if ($item->getAbusiva() > $newStock) {
                $restante = $newStock - $item->getAbusiva();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getAbusiva() < $newStock) {
                $restante = $newStock - $item->getAbusiva();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setAbusiva($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'signoriOtto':
              if ($item->getSignoriOtto() > $newStock) {
                $restante = $newStock - $item->getSignoriOtto();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getSignoriOtto() < $newStock) {
                $restante = $newStock - $item->getSignoriOtto();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setSignoriOtto($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            case 'gianottoModas':
              if ($item->getGianottoModas() > $newStock) {
                $restante = $newStock - $item->getGianottoModas();
                $valorRestante = - ($restante);
                $novoValor = $total - $valorRestante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }

              if ($item->getGianottoModas() < $newStock) {
                $restante = $newStock - $item->getGianottoModas();
                $novoValor = $total + $restante;

                $newQuantity = $item->setTotal($novoValor);
                $entityManager->persist($newQuantity);
                $entityManager->flush();
              }
              $item->setGianottoModas($newStock);
              $entityManager->persist($item);
              $entityManager->flush();
              break;

            default:
              throw new \Exception("Selecione a doca", 404);
              break;
          }
        }
      }
    }
    return $response->withStatus(200);
  }
  public function somaFabricaLojista($newStock, $cvto)
  {
    return $newStock + $cvto;
  }
}
