<?php

namespace auctionforge\inc\abs;

use auctionforge\inc\Debug;
use auctionforge\inc\helpers\LotHelper;
use auctionforge\inc\helpers\LotLogging;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\model\Lot;

abstract class ImageModel
{
    const LABEL_ORIGINAL = 'original';
    const LABEL_CUSTOM = 'custom';
    const LABEL_MIN = 'min';
    const LABEL_MEDIUM = 'medium';
    const LABEL_ATTACH = 'attachment';

    const PART_PATH_CLONED_IMAGES = '/cloned-images/';

    /**
     * Site Record ID
     * @var int
     */
    protected $postId;

    /**
     * Lot instance
     * @var LotHelper
     */
    protected $lot;

    /**
     * @var LotLogging
     */
    protected $logger;

    /**
     * Image list
     * @var array
     */
    protected $imageList;

    /**
     * Options for downloadable images from bidspirit system
     * @var array
     */
    protected $imageParams;

    /**
     * @var boolean
     */
    protected $postThumbnailSet = false;

    /**
     * Image prefix
     * @var string
     */
    protected $prefixImage;

    protected $imageCloned;

    const IMAGE_HOST_CLONED = 'https://bidspirit-images.global.ssl.fastly.net/';
    const IMAGE_HOST = 'https://res.cloudinary.com/bidspirit/image/upload/';
    const HOST_FILES = 'https://bidspirit-uploads-1.global.ssl.fastly.net/';

    /**
     * Get post id
     * @return int
     */
    public function getPostId()
    {
        return $this->postId;
    }

    /**
     * Setting post id
     * @param int $postId
     */
    protected function setPostId($postId)
    {
        $this->postId = $postId;
    }

    /**
     * Get instance Lot
     * @return LotHelper
     */
    public function getLot()
    {
        return $this->lot;
    }

    /**
     * Set instance Lot
     * @param LotHelper $lot
     */
    protected function setLot($lot)
    {
        $this->lot = $lot;
    }

    /**
     * Get Image list
     * @return array
     */
    public function getImageList()
    {
        return $this->imageList;
    }

    /**
     * Set image list
     * @return void
     */
    protected function setImageList()
    {
        $this->imageList = array();
        $imageList = apply_filters('auctionforge_before_set_image_list', $this->getLot()->getImagesListStr(), $this->getLot());

        if (!empty($imageList)) {
            $imageList = explode(',', $imageList);
            if (!empty($imageList) && is_array($imageList)) {
                foreach ($imageList as $imageName) {
                    $this->imageList[] = $this->prepareUrlToImage($imageName);
                }
            }
        }
        $this->imageList = apply_filters('auctionforge_after_set_image_list', $this->imageList, $this->getLot());
    }

    protected function initImageParams()
    {
        if ($this->isImageCloned()) {
            $this->setImageParams($this->getClonedImageParams());
        } else {
            $this->setImageParams($this->getNonClonedImageParams());
        }
    }

    private function getClonedImageParams()
    {
        return array(
            0 => 'a_ignore_q_80_w_1000_c_limit_',
            self::LABEL_MIN => 'a_ignore_q_80_h_100_c_limit_',
            self::LABEL_MEDIUM => 'a_ignore_q_80_w_400_h_400_c_fit_',
        );
    }

    private function getNonClonedImageParams()
    {
        return array(
            0 => 'q_80,w_1000,c_limit',
            self::LABEL_MIN => 'q_80,h_100,c_limit',
            self::LABEL_MEDIUM => 'q_80,h_400,c_limit'
        );
    }

    /**
     * Prepare url for images
     * @param $image
     * @return string[]
     */
    protected function prepareUrlToImage($image)
    {
        $size = $this->getImageParams();
        if ($this->isImageCloned()) {
            $imgIndex = preg_replace('/\D+/', '', $image);
            return array(
                self::LABEL_ORIGINAL => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getImagesBase() .
                    '/' . $imgIndex . '/' . $image,
                self::LABEL_CUSTOM => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getImagesBase() .
                    '/' . $imgIndex . '/' . $size[0] . $image,
                self::LABEL_MIN => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getImagesBase() .
                    '/' . $imgIndex . '/' . $size[self::LABEL_MIN] . $image,
                self::LABEL_MEDIUM => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getImagesBase() .
                    '/' . $imgIndex . '/' . $size[self::LABEL_MEDIUM] . $image
            );
        } else {
            return array(
                self::LABEL_ORIGINAL => self::IMAGE_HOST . $this->getPrefixImage() . '_' . $this->getLot()->getImagesBase() . '_' . $image,
                self::LABEL_CUSTOM => self::IMAGE_HOST . $size[0] . '/' . $this->getPrefixImage() . '_' . $this->getLot()
                        ->getImagesBase() . '_' . $image,
                self::LABEL_MIN => self::IMAGE_HOST . $size[self::LABEL_MIN] . '/' . $this->getPrefixImage() . '_' . $this->getLot()
                        ->getImagesBase() . '_' . $image,
                self::LABEL_MEDIUM => self::IMAGE_HOST . $size[self::LABEL_MEDIUM] . '/' . $this->getPrefixImage() . '_' . $this->getLot()
                        ->getImagesBase() . '_' . $image
            );
        }
    }

    public function prepareUrlToTemplate()
    {
        $size = $this->getClonedImageParams();
        $template = array();
        $template['cloned'] = array(
            self::LABEL_ORIGINAL => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getPostId() .
                '/$2/$1',
            self::LABEL_CUSTOM => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getImagesBase() .
                '/$2/' . $size[0] . '$1',
            self::LABEL_MIN => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getImagesBase() .
                '/$2/' . $size[self::LABEL_MIN] . '$1',
            self::LABEL_MEDIUM => self::IMAGE_HOST_CLONED . $this->getPrefixImage() . self::PART_PATH_CLONED_IMAGES . $this->getLot()->getImagesBase() .
                '/$2/' . $size[self::LABEL_MEDIUM] . '$1'
        );
        $size = $this->getNonClonedImageParams();
        $template['uncloned'] = array(
            self::LABEL_ORIGINAL => self::IMAGE_HOST . $this->getPrefixImage() . '_' . $this->getLot()->getPostId() . '_' . '$1',
            self::LABEL_CUSTOM => self::IMAGE_HOST . $size[0] . '/' . $this->getPrefixImage() . '_' . $this->getLot()
                    ->getImagesBase() . '_' . '$1',
            self::LABEL_MIN => self::IMAGE_HOST . $size[self::LABEL_MIN] . '/' . $this->getPrefixImage() . '_' . $this->getLot()
                    ->getImagesBase() . '_' . '$1',
            self::LABEL_MEDIUM => self::IMAGE_HOST . $size[self::LABEL_MEDIUM] . '/' . $this->getPrefixImage() . '_' . $this->getLot()
                    ->getImagesBase() . '_' . '$1'
        );
        return htmlentities(json_encode($template, ENT_QUOTES));
    }

    /**
     * Get image params
     * @return array
     */
    public function getImageParams()
    {
        return $this->imageParams;
    }

    /**
     * Set image params
     * @param array $imageParams
     */
    protected function setImageParams($imageParams)
    {
        $this->imageParams = $imageParams;
    }

    /**
     * Get prefix image
     * @return string
     */
    public function getPrefixImage()
    {
        return $this->prefixImage;
    }

    /**
     * Set prefix image
     */
    protected function setPrefixImage()
    {
        $alias = 'account';
        $account = SettingsHelper::getOption('server', '');
        preg_match('/:\/\/(?\'' . $alias . '\'[\w\W]+)\./U', $account, $output);
        if (!empty($output[$alias])) {
            $this->prefixImage = $output[$alias];
        } else {
            $this->prefixImage = '';
        }
    }

    /**
     * @return boolean
     */
    public function isImageCloned()
    {
        return Model::toBoolean($this->imageCloned);
    }

    /**
     * @param mixed $imageCloned
     */
    public function setImageCloned($imageCloned)
    {
        $this->imageCloned = $imageCloned;
    }

    /**
     * @return LotLogging
     */
    public function instanceLogger()
    {
        return $this->logger;
    }

    /**
     * @param LotLogging $logger
     */
    public function setLogger($logger)
    {
        $this->logger = $logger;
    }

    /**
     * @return mixed
     */
    public function isPostThumbnailSet()
    {
        return $this->postThumbnailSet;
    }

    /**
     * @param mixed $postThumbnailSet
     */
    public function setPostThumbnailSet($postThumbnailSet)
    {
        $this->postThumbnailSet = $postThumbnailSet;
    }
}