<?php

namespace app\models\base;

use yii\base\Model;
use yii\db\ActiveRecord;
use yii\helpers\Url;

/**
 * @property string $file_name
 * @property string $url
 * @property string $file_title
 */

class Image extends Model
{
    const SIZE_1040x586 = 1;
    const SIZE_680x383 = 2;
    const SIZE_500x282 = 3;
    const SIZE_320x180 = 4;
    const SIZE_320alt  = 5;
    const SIZE_HD  = 6;
    const SIZE_ORIGINAL = 0;

	const PREFIX_IMG = [self::SIZE_320x180=>'size4', self::SIZE_320alt=>'320', self::SIZE_500x282=>'size3', self::SIZE_680x383=>'size2', self::SIZE_1040x586=>'size1', self::SIZE_HD=>'sizehd', self::SIZE_ORIGINAL=>'origin', self::SIZE_1040x586.'_'=>'size1', self::SIZE_680x383.'_'=>'size2'];

    public $types = ["webp","jpg"];
    public $post_id = null;
    public $type = NULL;
	public $photo_name = '';



    public function getUrl_($size = 1,$type = NULL,$full = true)
    {
        $this->type =  $type ?? $this->type;

        switch ($this->type){
            case "jpg":
				if( $size > 4 ){
					//echo \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg"; return;
					if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg" ) ){
						$url = "/images/news/news/{$this->post_id}_{$size}.jpg";
					}else if( file_exists( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_320.jpg" )){
                        $url =  "/images/news/news/{$this->post_id}_320.jpg";
					}
                    $url =  "/images/default.jpg";
				}
				if( $url = ( \Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpg" ) ){
					return "/images/news/news/{$this->post_id}_size{$size}.jpg";
				}
                $url =  '';
                break;
            default:
                if(file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_size{$size}.webp")) $url =  "/images/news/webp/{$this->post_id}_size{$size}.webp";
                elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpg")) $url =  "/images/news/news/{$this->post_id}_size{$size}.jpg";
                elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_size{$size}.jpeg")) $url =  "/images/news/news/{$this->post_id}_size{$size}.jpeg";
                elseif (file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$size}.jpg")) $url =  "/images/news/news/{$this->post_id}_{$size}.jpg";
                else $url =  "/images/news/webp/{$this->post_id}_size{$size}.webp";
        }
        $base = Url::base('https');
        return ($full?$base:"").$url;
    }

    public function getUrl($size = 1,$type = NULL,$full = true)
    {
        $this->type =  $type ?? $this->type;
		$url = $this->getSomebody($size, $this->type);
		//$url = '/images/default.jpeg';
		if( $url === false ) return false;
		$base = Url::base('https');
		if( stripos( $base,'amic.ru' ) === false ) $full = false;
//		if(  IMG_GLOB == true ){
//			$full = true;
//			$base = 'https://www.amic.ru';
//		}
        return ($full?$base:"").$url;
	}

	public function getSomebody($one, $type){
		$f = false;
		foreach( self::PREFIX_IMG as $key => $pref ){
			$key = str_replace( '_', '', $key );
			if( $key == $one ) $f = true;
			if( $f ){
				if( $type == 'webp' ){
					if( file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp")  ){
						return "/images/news/webp/{$this->post_id}_{$pref}.webp";
					}else{
						if( file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg")  ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
					}
				}
				if( $type == 'jpg' && file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg" )  ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
			}
		}
		foreach( self::PREFIX_IMG as $key => $pref ){
			if( $key == $one ) break;
			if( $type == 'webp' && file_exists(\Yii::getAlias('@webroot')."/images/news/webp/{$this->post_id}_{$pref}.webp")  ){
				return "/images/news/webp/{$this->post_id}_{$pref}.webp";
			}else{
				if( $type == 'webp' && file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg")  ) return "/images/news/news/{$this->post_id}_{$pref}.jpg";
			}
			if( $type == 'jpg' && file_exists(\Yii::getAlias('@webroot')."/images/news/news/{$this->post_id}_{$pref}.jpg")  )  return "/images/news/news/{$this->post_id}_{$pref}.jpg";
		}
		if( isset( $this->photo_name ) && $this->photo_name != '' ){
			if(  is_numeric( $this->photo_name  ) && is_int( $this->photo_name*1 ) ){
				$obj = new image();
				$obj = $obj->findOne($this->photo_name*1);
				return $obj->getUrl($one, $type, false);
			}
			return $this->photo_name;
		}
		return false;
	}
    public function getIcon(){
		if( isset( $this->photo_name ) && $this->photo_name != '' ){
			if(  is_numeric( $this->photo_name  ) && is_int( $this->photo_name*1 ) ){
				return "/images/news/news/{$this->photo_name}.jpg";
			}
			return $this->photo_name;
		}
		 return "/images/news/news/{$this->post_id}.jpg";
	}
    public static function find(){
        $model = new static();
        return $model;
    }
    public static function findOne($id,$type="webp",$photo_name=""){
        $model = self::find();
        $model->type = $type;
        $model->post_id = $id;
		$model->photo_name = $photo_name;
        return $model;
    }
}
