<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use PHPUnit\Framework\Constraint\Count;

/**
 * App\Models\Country
 *
 * @property int $id
 * @property string $code
 * @property string $phone
 * @property string $en
 * @property string $de
 * @property string $es
 * @property string $fr
 * @property string $it
 * @property string $ru
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereDe($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereEn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereEs($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereFr($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereIt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereRu($value)
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country query()
 * @property int|null $active
 * @property array|null $trans
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereTrans($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereUpdatedAt($value)
 * @property string|null $trans_name
 * @property array|null $attr
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereAttr($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereTransName($value)
 * @property bool|null $switch
 * @property bool|null $own_eur
 * @property bool|null $currency
 * @property string|null $currency_unit
 * @property bool|null $currency_calc
 * @property float|null $currency_faktor
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereCurrency($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereCurrencyCalc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereCurrencyFaktor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereCurrencyName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereOwnEur($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereSwitch($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\CountryPrice[] $country_prices
 * @property-read int|null $country_prices_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Country whereCurrencyUnit($value)
 */
class Country extends Model
{
    protected $table = 'countries';

    protected $casts = [
        'switch' => 'bool',
        'own_eur' => 'bool',
        'currency' => 'bool',
        'currency_calc' => 'bool',
        'trans_name' => 'array',
        'attr' => 'array'
    ];

    protected $fillable = [
        'code', 'phone', 'en', 'de', 'es', 'fr', 'it', 'ru',
        'switch', 'own_eur', 'currency', 'currency_unit', 'currency_calc', 'currency_faktor',
        'active', 'trans_name', 'attr',
    ];

    public function country_prices()
    {
        return $this->hasMany(CountryPrice::class, 'country_id');
    }

    public function getLocated($lang = false){

        if(!$lang){
            $lang = \App::getLocale();

        }
        if($lang === 'de'){
            return $this->de;
        }
        if($lang === 'en'){
            return $this->en;
        }
        if($lang === 'es'){
            return $this->es;
        }
        if($lang === 'fr'){
            return $this->fr;
        }
        if($lang === 'it'){
            return $this->it;
        }
        if($lang === 'ru'){
            return $this->ru;
        }

        //search by trans

        if($val = $this->getTrans('name', $lang)){
            return $val;
        }
        return $this->de;
    }

    public function getTrans($key, $lang)
    {
        $key = 'trans_' . $key;
        if (!empty($this->{$key}[$lang])) {
            return $this->{$key}[$lang];
        }
        return "";
    }

    public function getAttrByKey($key)
    {
        $name = 'attr';
        if (!empty($this->{$name}[$key])) {
            return $this->{$name}[$key];
        }
        return "";
    }

    public static function getCountryIdByCode($code){
        if($code == null){
            return null;
        }
        $r = Country::where('code', '=', $code)->first();
        if($r){
            return $r->id;
        }
        return null;
    }

    public static function getCountryIdByCodeOrOne($code){
        if($code == null){
            return 1;
        }
        $r = Country::where('code', '=', $code)->first();
        if($r){
            return $r->id;
        }
        return 1;
    }

    public static function getCountryIdByPhone($phone){
        if($phone == null){
            return null;
        }
        $r = Country::where('phone', '=', $phone)->first();
        if($r){
            return $r->id;
        }
        return null;
    }

}
