<?php

namespace App\Models;

class PropertyContact extends BaseModel
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'property_contact';
    protected $appends = ['view_full_phone', 'view_full_mobile', 'view_full_mobile2', 'view_full_fax', 'formattedMobileNumber'];

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    protected $dateFormat = 'U';
    protected $guarded = [];
    const CHARACTER_LENGTH = 6;
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];


    public function getViewFullPhoneAttribute()
    {
        try {
            $response = null;

            if(strpos($this->phone,'444') !== false) {
                $phoneNumber = $this->phone;
                preg_match_all('/(444).*/m', $this->phone, $matchesPhone, PREG_SET_ORDER, 0);
                $matchesPhone = reset($matchesPhone);
                $matchesPhone = reset($matchesPhone);
                $matchesPhone = str_replace(' ','', $matchesPhone);

                $phoneNumber = substr($matchesPhone,0,3).' '.substr($matchesPhone,3,1).' '.substr($matchesPhone,4,3);

                return $phoneNumber;
            }

            if ($this->phone == null || $this->phone == "" || strlen($this->phone) < self::CHARACTER_LENGTH) {
                return null;
            }

            if ($this->phone_code) {
                $response = strpos($this->phone_code, '+') === false ? '+' . $this->phone_code . $this->phone : $this->phone_code . $this->phone;
            } else {
                $response = strpos($this->phone, '+') === false ? '+' . $this->phone : $this->phone;
            }
            return $response;
        } catch (Exception $e) {
            return null;
        }
    }

    public function getViewFullMobileAttribute()
    {
        try {
            $response = null;

            if ($this->mobile == null || $this->mobile == "" || strlen($this->mobile) < self::CHARACTER_LENGTH) {
                return null;
            }
            if ($this->mobile_code) {
                $response = strpos($this->mobile_code, '+') === false ? '+' . $this->mobile_code . $this->mobile : $this->mobile_code . $this->mobile;
            } else {
                $response = strpos($this->mobile, '+') === false ? '+' . $this->mobile : $this->mobile;
            }
            return $response;
        } catch (Exception $e) {
            return null;
        }
    }


    public function getViewFullMobile2Attribute()
    {
        try {
            $response = null;
            if ($this->mobile2 == null || $this->mobile2 == "" || strlen($this->mobile2) < self::CHARACTER_LENGTH) {
                return null;
            }

            if ($this->mobile2_code) {
                $response = strpos($this->mobile2_code, '+') === false ? '+' . $this->mobile2_code . $this->mobile2 : $this->mobile2_code . $this->mobile2;
            } else {
                $response = strpos($this->mobile2, '+') === false ? '+' . $this->mobile2 : $this->mobile2;
            }
            return $response;

        } catch (Exception $e) {
            return null;
        }
    }


    public function getViewFullFaxAttribute()
    {
        try {
            $response = null;
            if ($this->fax == null || $this->fax == "" || strlen($this->fax) < self::CHARACTER_LENGTH) {
                return null;
            }
            if ($this->fax_code) {
                $response = strpos($this->fax_code, '+') === false ? '+' . $this->fax_code . $this->fax : $this->fax_code . $this->fax;
            } else {
                $response = strpos($this->fax, '+') === false ? '+' . $this->fax : $this->fax;
            }
            return $response;
        } catch (Exception $e) {
            return null;
        }
    }

    public function getFormattedMobileNumberAttribute()
    {
        try {
            $mobilePhoneNumber = null;
            if ($this->mobile == null || $this->mobile == "" || strlen($this->mobile) < self::CHARACTER_LENGTH) {
                return null;
            }
            if ($this->mobile) {
                $this->mobile = preg_replace('/[^0-9,.]+/i', '', $this->mobile);
                $mobilePhoneNumber = $this->mobile_code . $this->mobile;
            }
            return $mobilePhoneNumber;
        } catch (Exception $e) {
            return null;
        }
    }


}
