<?php
/**
 * NOTICE OF LICENSE.
 *
 * Copyright 2022 Kosmonaft.dev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *  @author    Alexandre DEBUSSCHÈRE <alexandre@kosmonaft.dev>
 *  @copyright 2022 - 2023 Kosmonaft.dev
 *  @license   MIT
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * Class MRMaxAddress
 */
class MRMaxAddress extends Address
{

    /**
     * @param array $pickup
     * @return int|false
     */
    public static function createAddressFromPickup($pickup, Customer $customer, Address $delivery_address)
    {
        $pickup = MRMaxTools::jsonDecode(
            MRMaxTools::jsonEncode($pickup)
        );

        if (!is_object($pickup) || !property_exists($pickup, 'LgAdr1')) {
            MRMaxTools::log(sprintf(
                '%s: $pickup not an object (%s) (%s) OR does not have property "LgAdr1"',
                __METHOD__,
                gettype($pickup),
                MRMaxTools::jsonEncode($pickup)
            ));
            return false;
        }

        // Check phone number is not empty
        if (!Tools::strlen($delivery_address->phone) && Tools::strlen($delivery_address->phone_mobile)) {
            $delivery_address->phone = $delivery_address->phone_mobile;
        }

        $address = new Address();
        $address->id_customer = $customer->id;
        $address->firstname = $delivery_address->firstname;
        $address->lastname = $delivery_address->lastname;
        $address->company = $pickup->LgAdr1;
        $address->address1 = $pickup->LgAdr3;
        $address->postcode = $pickup->CP;
        $address->city = $pickup->Ville;
        $address->id_country = Country::getByIso($pickup->Pays);
        $address->country = Country::getNameById(Context::getContext()->language->id, $address->id_country);
        $address->phone = MRMaxTools::formatPhoneNumber($delivery_address->phone, $pickup->Pays);
        $address->alias = md5($pickup->Num.$pickup->LgAdr1);
        $address->active = true;
        $address->deleted = true;
        $address->other = $pickup->Num;
        $address->add();

        if (!Validate::isLoadedObject($address)) {
            return false;
        }

        return (int)$address->id;
    }
}
