<?php

namespace Autogedal\Retur\ViewModel;

class CustomHelper implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    protected $resource;
    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Framework\Registry $registry
    ) {
        $this->_coreRegistry = $registry;
        $this->resource = $resource;
    }

    public function isEditPage()
    {
        return $this->_coreRegistry->registry('edit_page');
    }

    public function getCurrentReturId()
    {
        return $this->_coreRegistry->registry('retur_id');
    }

    public function returItemBelongToCurrentRetur($itemId)
    {
        $currentReturId = $this->getCurrentReturId();
        if (!$currentReturId) {
            return true;
        }

        $connection = $this->resource->getConnection();
        $returItemTable = $this->resource->getTableName('autogedal_retur_item');

        $select = $connection->select();
        $select->from(
            ['e' => $returItemTable],
            ['entity_id', 'retur_id', 'item_id', 'qty']
        )->where(
            'e.item_id = ?',
            $itemId
        );

        if ($currentReturId) {
            $select->where(
                'e.retur_id = ?',
                $currentReturId
            );
        }

        $returItem = $connection->fetchAll($select);

        if (empty($returItem)) {
            return false;
        }
        return true;
    }
    
    public function getReturIdByIndex($itemId, $index)
    {
        $connection = $this->resource->getConnection();
        $returItemTable = $this->resource->getTableName('autogedal_retur_item');

        $select = $connection->select();
        $select->from(
            ['e' => $returItemTable],
            ['entity_id', 'retur_id', 'item_id', 'qty']
        )->where(
            'e.item_id = ?',
            $itemId
        );

        $returItem = $connection->fetchAll($select);
        if (!empty($returItem) && isset($returItem[$index])) {
            return $returItem[$index]['retur_id'];
        }
        return false;
    }

    public function getReturIdForAnotherRetur($itemId)
    {
        $connection = $this->resource->getConnection();
        $returItemTable = $this->resource->getTableName('autogedal_retur_item');

        $select = $connection->select();
        $select->from(
            ['e' => $returItemTable],
            ['entity_id', 'retur_id', 'item_id', 'qty']
        )->where(
            'e.item_id = ?',
            $itemId
        );

        if ($this->getCurrentReturId()) {
            $select->where(
                'e.retur_id != ?',
                $this->getCurrentReturId()
            );
        }

        $returItem = $connection->fetchAll($select);
        if (!empty($returItem)) {
            return $returItem[0]['retur_id'];
        }
        return false;
    }

    public function getReturItemQty($itemId)
    {
        $connection = $this->resource->getConnection();
        $returItemTable = $this->resource->getTableName('autogedal_retur_item');

        $select = $connection->select();
        $select->from(
            ['e' => $returItemTable],
            ['entity_id', 'retur_id', 'item_id', 'qty']
        )->where(
            'e.item_id = ?',
            $itemId
        );

        if ($this->getCurrentReturId()) {
            $select->where(
                'e.retur_id = ?',
                $this->getCurrentReturId()
            );
        }

        $returItem = $connection->fetchAll($select);
        
        if ($returItem) {
            $qty = 0;
            foreach ($returItem as $key => $info) {
                $qty += $info['qty'];
            }
            return $qty;
        }
        
        return false;
    }

    public function returItemExist($itemId)
    {
        $connection = $this->resource->getConnection();
        $returItemTable = $this->resource->getTableName('autogedal_retur_item');
        $returId = $this->_coreRegistry->registry('retur_id');

        $select = $connection->select();
        $select->from(
            ['e' => $returItemTable],
            ['entity_id', 'retur_id', 'item_id']
        )->where(
            'e.item_id = ?',
            $itemId
        );
        if ($this->isEditPage()) {
            $select->where(
                'e.retur_id = ?',
                $returId
            );
        }

        $returItem = $connection->fetchOne($select);
        
        return $returItem ? true : false;
    }

    public function getReturIdByItemId($itemId)
    {
        $connection = $this->resource->getConnection();
        $returItemTable = $this->resource->getTableName('autogedal_retur_item');

        $select = $connection->select();
        $select->from(
            ['e' => $returItemTable],
            ['retur_id']
        )->where(
            'e.item_id = ?',
            $itemId
        );
        
        return $connection->fetchOne($select);
    }

    public function getItemQty($itemId)
    {
        $connection = $this->resource->getConnection();
        $returItemTable = $this->resource->getTableName('autogedal_retur_item');

        $select = $connection->select();
        $select->from(
            ['e' => $returItemTable],
            ['qty']
        )->where(
            'e.item_id = ?',
            $itemId
        );
        
        $itemsReturned = $connection->fetchAll($select);
        if (empty($itemsReturned)) {
            return 0;
        }

        $qtyReturned = 0;
        foreach ($itemsReturned as $key => $info) {
            $qtyReturned += $info['qty'];
        }

        return $qtyReturned;
    }
    
}
