<?php

namespace WPIS;

class EstateProperties {

    static public $supportedLanguages = ['fr', 'nl', 'en'];

    static public $postTypeMetas;

    public function __construct(){

        self::$postTypeMetas = [
            [
                'id' => 'options',
                'label' => __('ImmoSync', 'wpis'),
                'icon' => 'fa-cogs',
                'callback' => 'wpisMetaboxBodyOptions',
                'fields' =>  [
                    [
                        'reference' => 'locked',
                        'label' => __('Do not update', 'wpis'),
                        'type'  => 'boolean'
                    ],
                    [
                        'reference' => 'undeletable',
                        'label' => __('Do not delete', 'wpis'),
                        'type'  => 'boolean'
                    ],
                ],
            ],
            [
                'id' => 'general',
                'label' => __('General', 'wpis'),
                'icon' => 'fa-home',
                'callback' => 'wpisMetaboxBodyGeneral',
                'fields' =>  [
                    [
                        'reference' => 'id',
                        'label' => __('Estate ID', 'wpis'),
                        'type'  => 'readonly'
                    ],
                    [
                        'reference' => 'name',
                        'label' => __('Estate title', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'reference',
                        'label' => __('Reference', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'category.label',
                        'label' => __('Category', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'subcategory.label',
                        'label' => __('Sub-category', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'purpose.label',
                        'label' => __('Transaction type', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'status.label',
                        'label' => __('Transaction status', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'links.virtualVisit',
                        'label' => __('Virtual visit link', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'links.video',
                        'label' => __('Video link', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'links.model',
                        'label' => __('3D model link', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'links.appointment',
                        'label' => __('Appointment link', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'links.custom',
                        'label' => __('Custom links', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'properties.hasChildren',
                        'label' => __('Parent project', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'properties.hasParent',
                        'label' => __('Child project', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'properties.parentId',
                        'label' => __('Parent project ID', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'properties.isFavorite',
                        'label' => __('Showcase', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'properties.isFurnished',
                        'label' => __('Furnished', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ]
                ]
            ],
            [
                'id' => 'finance',
                'label' => __('Finance', 'wpis'),
                'icon' => 'fa-euro-sign',
                'callback' => 'wpisMetaboxBodyFinance',
                'fields' =>  [
                    [
                        'reference' => 'finance.price',
                        'label' => __('Price', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'price'
                    ],
                    [
                        'reference' => 'finance.priceChangedAt',
                        'label' => __('Price change at', 'wpis'),
                        'type'  => 'date'
                    ],
                    [
                        'reference' => 'finance.availableAt',
                        'label' => __('Available at', 'wpis'),
                        'type'  => 'date'
                    ],
                    [
                        'reference' => 'finance.availability',
                        'label' => __('Availability', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'finance.currency',
                        'label' => __('Currency', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'finance.annuityMonthly',
                        'label' => __('Annuity monthly', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.cadastre',
                        'label' => __('Cadastre', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'price'
                    ],
                    [
                        'reference' => 'finance.propertyTax',
                        'label' => __('Property Tax', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.chargeOwner',
                        'label' => __('Charge owner', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.chargeRenter',
                        'label' => __('Charge renter', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.fieldValue',
                        'label' => __('Field value', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.constructionValue',
                        'label' => __('Construction value', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.notarialDeedValue',
                        'label' => __('Notarial deep value', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.garageUnitPrice',
                        'label' => __('Garage unit price', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.parkingUnitPrice',
                        'label' => __('Parking unit price', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.parkingInteriorUnitPrice',
                        'label' => __('Interior parking unit price', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.parkingExteriorUnitPrice',
                        'label' => __('Exterior parking unit price', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'finance.offer',
                        'label' => __('Make an offer', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'finance.hidden',
                        'label' => __('Hidden price', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'building.constructionYear',
                        'label' => __('Construction year', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'building.renovationYear',
                        'label' => __('Renovation year', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'building.orientation',
                        'label' => __('Building orientation', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'building.gardenOrientation',
                        'label' => __('Garden orientation', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'building.terraceOrientation',
                        'label' => __('Terrace orientation', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'building.state',
                        'label' => __('Building state', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                ]
            ],
            [
                'id' => 'surfaces',
                'label' => __('Areas', 'wpis'),
                'icon' => 'fa-expand',
                'callback' => 'wpisMetaboxBodySurface',
                'fields' =>  [
                    [
                        'reference' => 'areas.total',
                        'label' => __('Total area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.ground',
                        'label' => __('Field area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.garden',
                        'label' => __('Garden area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.living',
                        'label' => __('Living area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.diningroom',
                        'label' => __('Dinning room area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.kitchen',
                        'label' => __('Kitchen area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.bathroom',
                        'label' => __('Bathroom area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.bathroom2',
                        'label' => __('Bathroom 2 area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.cellar',
                        'label' => __('Cellar area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.washroom',
                        'label' => __('Washroom area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.dressing',
                        'label' => __('Dressing area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.office',
                        'label' => __('Office area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.bedroom1',
                        'label' => __('Bedroom 1 area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.bedroom2',
                        'label' => __('Bedroom 2 area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.bedroom3',
                        'label' => __('Bedroom 3 area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.bedroom4',
                        'label' => __('Bedroom 4 area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.bedroom5',
                        'label' => __('Bedroom 5 area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.showroom',
                        'label' => __('Showroom area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.storage',
                        'label' => __('Storage area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.terrace',
                        'label' => __('Terrace area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.manufacturing',
                        'label' => __('Manufacturing area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ],
                    [
                        'reference' => 'areas.displayWindow',
                        'label' => __('Vitrine area', 'wpis'),
                        'type'  => 'number',
                        'filter' => 'decimal',
                        'unit' => 'm²'
                    ]
                ]
            ],
            [
                'id' => 'energy',
                'label' => __('Energy reports', 'wpis'),
                'icon' => 'fa-leaf',
                'callback' => 'wpisMetaboxBodyEnergy',
                'fields' =>  [
                    [
                        'reference' => 'energy.epcCode',
                        'label' => __('EPC Code', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'energy.epcLabel',
                        'label' => __('EPC Label', 'wpis'),
                        'type'  => 'text',
                        'filter' => 'epc'
                    ],
                    [
                        'reference' => 'energy.epcValue',
                        'label' => __('EPC Value', 'wpis'),
                        'type'  => 'number',
                        'unit' => __('kWh/m²/year', 'wpis')
                    ],
                    [
                        'reference' => 'energy.epcTotal',
                        'label' => __('EPC Total', 'wpis'),
                        'type'  => 'number',
                        'unit' => __('kWh/year', 'wpis')
                    ],
                    [
                        'reference' => 'energy.gazEmit',
                        'label' => __('Gaz emition', 'wpis'),
                        'type'  => 'number',
                        'unit' => __('Kg/CO2/year', 'wpis')
                    ],
                    [
                        'reference' => 'energy.heatingType',
                        'label' => __('Heating type', 'wpis'),
                        'type'  => 'text',
                        'translatable' => true
                    ],
                    [
                        'reference' => 'energy.solarElectricity',
                        'label' => __('Solar energy', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'energy.electricityConformity',
                        'label' => __('Electricity conformityé', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ]
                ]
            ],
            [
                'id' => 'address',
                'label' => __('Address', 'wpis'),
                'icon' => 'fa-location-arrow',
                'callback' => 'wpisMetaboxBodyAddress',
                'fields' =>  [
                    [
                        'reference' => 'address.street',
                        'label' => __('Street', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'address.number',
                        'label' => __('Number', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'address.box',
                        'label' => __('Box', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'address.zip',
                        'label' => __('Zip', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'address.city',
                        'label' => __('City', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'address.country',
                        'label' => __('Country', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'address.longitude',
                        'label' => __('Longitude', 'wpis'),
                        'type'  => 'text'
                    ],
                    [
                        'reference' => 'address.latitude',
                        'label' => __('Latitude', 'wpis'),
                        'type'  => 'text'
                    ]
                ]
            ],
            [
                'id' => 'proximity',
                'label' => __('Proximity', 'wpis'),
                'icon' => 'fa-route',
                'callback' => 'wpisMetaboxBodyProximity',
                'fields' =>  [
                    [
                        'reference' => 'proximity.highway',
                        'label' => __('Highway', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.transports',
                        'label' => __('Transport', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.station',
                        'label' => __('Station', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.bus',
                        'label' => __('Bus', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.metro',
                        'label' => __('Metro', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.tram',
                        'label' => __('Tram', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.school',
                        'label' => __('School', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.stores',
                        'label' => __('Stores', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.sportCenter',
                        'label' => __('Sport center', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'proximity.beach',
                        'label' => __('Beach', 'wpis'),
                        'type'  => 'number'
                    ]
                ]
            ],
            [
                'id' => 'commodities',
                'label' => __('Commodities', 'wpis'),
                'icon' => 'fa-star',
                'callback' => 'wpisMetaboxBodyCommodities',
                'fields' =>  [
                    [
                        'reference' => 'configuration.floorCurrent',
                        'label' => __('Current floor', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'configuration.floorTotal',
                        'label' => __('Total floor', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'configuration.bedrooms',
                        'label' => __('Bedrooms', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'configuration.kitchens',
                        'label' => __('Kitchen', 'wpis'),
                        'type'  => 'string'
                    ],
                    [
                        'reference' => 'configuration.toilets',
                        'label' => __('Toilets count', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'configuration.bathrooms',
                        'label' => __('Bathrooms count', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'configuration.showerrooms',
                        'label' => __('Showerrooms count', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'configuration.garages',
                        'label' => __('Garages count', 'wpis'),
                        'type'  => 'number'
                    ],
                    [
                        'reference' => 'configuration.terraces',
                        'label' => __('Terrace', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'configuration.cave',
                        'label' => __('Cave', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'configuration.parking',
                        'label' => __('Parking', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ],
                    [
                        'reference' => 'configuration.garden',
                        'label' => __('Garden', 'wpis'),
                        'type'  => 'boolean',
                        'filter' => 'yes/no'
                    ]
                ]
            ],
            [
                'id' => 'files',
                'label' => __('Files', 'wpis'),
                'icon' => 'fa-file-pdf',
                'callback' => 'wpisMetaboxBodyFiles',
                'fields' =>  [
                    [
                        'reference' => 'files',
                        'label' => __('Documents', 'wpis'),
                        'type'  => 'file-collection'
                    ]
                ]
            ],
            [
                'id' => 'hidden',
                'label' => null,
                'icon' => null,
                'callback' => null,
                'fields' =>  [
                    ['reference' => 'id'],
                    ['reference' => 'createdAt'],
                    ['reference' => 'updatedAt'],
                    ['reference' => 'purpose.id'],
                    ['reference' => 'status.id'],
                    ['reference' => 'description.title', 'translatable' => true],
                    ['reference' => 'description.base', 'translatable' => true],
                    ['reference' => 'description.short', 'translatable' => true],
                    ['reference' => 'description.long', 'translatable' => true],
                    ['reference' => 'amenities.generic', 'translatable' => true],
                    ['reference' => 'amenities.custom', 'translatable' => true],
                    ['reference' => 'agency.id'],
                    ['reference' => 'agency.name'],
                    ['reference' => 'agency.phone'],
                    ['reference' => 'agency.email'],
                    ['reference' => 'agency.addressFormatted'],
                    ['reference' => 'agent.firstname'],
                    ['reference' => 'agent.lastname'],
                    ['reference' => 'agent.email'],
                    ['reference' => 'agent.phone'],
                    ['reference' => 'agent.role'],
                    ['reference' => 'agent.picture'],
                    ['reference' => 'unmapped', 'translatable' => true]
                ]
            ]
        ];

    }

    static public function getMetaProperties( $meta ){
        $target = str_replace('wpis_', '', $meta);
        $target = str_replace('_', '.', $target);

        foreach(self::$postTypeMetas as $group){
            foreach($group['fields'] as $fieldDetails) {
                if( $fieldDetails['reference'] === $target ){
                    return $fieldDetails;
                }
            }
        }
    }

    static public function getPropertyLabel( $meta ){
        if( $properties = self::getMetaProperties($meta) ){
            return __($properties['label'], 'wpis');
        }
        return '';
    }

    static public function getValue( $postId, $meta, $language = '' ){
        $value = get_post_meta($postId, $meta . '_' . $language , true ) ?
                 get_post_meta($postId, $meta . '_' . $language , true ) :
                 get_post_meta($postId, $meta , true );

        return $value;
    }

    static public function getFormattedValue( $postId, $meta, $language = '', $filter = true, $showUnit = true ){
        $properties = self::getMetaProperties($meta);
        $value = self::getValue($postId, $meta, $language);
        $buffer = '';

        $hasCustomFilter = is_string($filter);
        $hasDefaultFilter = isset($properties['filter']);

        $filters = [
            'price'      => 'WPIS\PriceFilter',
            'decimal'    => 'WPIS\DecimalFilter',
            'number'     => 'WPIS\NumberFilter',
            'capitalize' => 'WPIS\CapitalizeFilter',
            'uppercase'  => 'WPIS\UppercaseFilter',
            'epc'        => 'WPIS\EpcImageFilter',
            'yes/no'     => 'WPIS\YesNoFilter'
        ];

        if( $filter && ($hasDefaultFilter || $hasCustomFilter) ){
            if( $hasDefaultFilter ) {
                $formattedValue = $filters[$properties['filter']]::transform($value);
                $buffer = $formattedValue;
            }

            if( $hasCustomFilter ){
                $formattedValue = $filters[$filter]::transform($value);
                $buffer = $formattedValue;
            }
        }
        else {
            $buffer = $value;
        }

        if( $buffer && $showUnit && isset($properties['unit']) ){
            $buffer .= '<span class="unit"> ' . __($properties['unit'], 'wpis') . '</span>';
        }

        return $buffer;
    }
}