<?php
/**
 * Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
 *
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
 * use, copy, modify, and distribute this software in source code or binary
 * form for use in connection with the web services and APIs provided by
 * Facebook.
 *
 * As with any software that integrates with the Facebook platform, your use
 * of this software is subject to the Facebook Developer Principles and
 * Policies [http://developers.facebook.com/policy/]. This copyright 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.
 *
 */

namespace FacebookAds\Object\ServerSide;

use ArrayAccess;


class CustomData implements ArrayAccess
{

    /**
     * Array of property to type mappings. Used for (de)serialization
     * @var string[]
     */
    protected static $param_types = array(
        'value' => 'float',
        'currency' => 'string',
        'content_name' => 'string',
        'content_category' => 'string',
        'content_ids' => 'string[]',
        'contents' => 'object[]',
        'content_type' => 'string',
        'order_id' => 'string',
        'predicted_ltv' => 'float',
        'num_items' => 'string',
        'status' => 'string',
        'search_string' => 'string'
    );
    /**
     * Array of attributes where the key is the local name, and the value is the original name
     * @var string[]
     */
    protected static $attributeMap = array(
        'value' => 'value',
        'currency' => 'currency',
        'content_name' => 'content_name',
        'content_category' => 'content_category',
        'content_ids' => 'content_ids',
        'contents' => 'contents',
        'content_type' => 'content_type',
        'order_id' => 'order_id',
        'predicted_ltv' => 'predicted_ltv',
        'num_items' => 'num_items',
        'status' => 'status',
        'search_string' => 'search_string'
    );
    /**
     * Array of attributes to setter functions (for deserialization of responses)
     * @var string[]
     */
    protected static $setters = array(
        'value' => 'setValue',
        'currency' => 'setCurrency',
        'content_name' => 'setContentName',
        'content_category' => 'setContentCategory',
        'content_ids' => 'setContentIds',
        'contents' => 'setContents',
        'content_type' => 'setContentType',
        'order_id' => 'setOrderId',
        'predicted_ltv' => 'setPredictedLtv',
        'num_items' => 'setNumItems',
        'status' => 'setStatus',
        'search_string' => 'setSearchString'
    );
    /**
     * Array of attributes to getter functions (for serialization of requests)
     * @var string[]
     */
    protected static $getters = array(
        'value' => 'getValue',
        'currency' => 'getCurrency',
        'content_name' => 'getContentName',
        'content_category' => 'getContentCategory',
        'content_ids' => 'getContentIds',
        'contents' => 'getContents',
        'content_type' => 'getContentType',
        'order_id' => 'getOrderId',
        'predicted_ltv' => 'getPredictedLtv',
        'num_items' => 'getNumItems',
        'status' => 'getStatus',
        'search_string' => 'getSearchString'
    );
    /**
     * Associative array for storing property values
     * @var mixed[]
     */
    protected $container = array();

    /**
     * Constructor
     * @param mixed[] $data Associated array of property value initializing the model
     */
    public function __construct(array $data = null)
    {
        $this->container['value'] = isset($data['value']) ? $data['value'] : null;
        $this->container['currency'] = isset($data['currency']) ? $data['currency'] : null;
        $this->container['content_name'] = isset($data['content_name']) ? $data['content_name'] : null;
        $this->container['content_category'] = isset($data['content_category']) ? $data['content_category'] : null;
        $this->container['content_ids'] = isset($data['content_ids']) ? $data['content_ids'] : null;
        $this->container['contents'] = isset($data['contents']) ? $data['contents'] : null;
        $this->container['content_type'] = isset($data['content_type']) ? $data['content_type'] : null;
        $this->container['order_id'] = isset($data['order_id']) ? $data['order_id'] : null;
        $this->container['predicted_ltv'] = isset($data['predicted_ltv']) ? $data['predicted_ltv'] : null;
        $this->container['num_items'] = isset($data['num_items']) ? $data['num_items'] : null;
        $this->container['status'] = isset($data['status']) ? $data['status'] : null;
        $this->container['search_string'] = isset($data['search_string']) ? $data['search_string'] : null;
    }

    public static function paramTypes()
    {
        return self::$param_types;
    }

    public static function attributeMap()
    {
        return self::$attributeMap;
    }

    public static function setters()
    {
        return self::$setters;
    }

    public static function getters()
    {
        return self::$getters;
    }

    /**
     * show all the invalid properties with reasons.
     *
     * @return array invalid properties with reasons
     */
    public function listInvalidProperties()
    {
        $invalid_properties = array();
        return $invalid_properties;
    }

    /**
     * validate all the properties in the model
     * return true if all passed
     *
     * @return bool True if all properteis are valid
     */
    public function valid()
    {
        return true;
    }

    /**
     * Sets value
     * @param float $value A numeric value associated with this event.
     *         This could be a monetary value or a value in some other metric.
     * @return $this
     */
    public function setValue($value)
    {
        $this->container['value'] = $value;

        return $this;
    }

    /**
     * Sets currency
     * @param string $currency The currency for the value specified, if applicable.
     *          Currency must be a valid ISO 4217 three digit currency code.
     * @return $this
     */
    public function setCurrency($currency)
    {
        $this->container['currency'] = $currency;

        return $this;
    }

    /**
     * Sets content_name
     * @param string $content_name The name of the page or product associated with the event. Example: 'lettuce'.
     * @return $this
     */
    public function setContentName($content_name)
    {
        $this->container['content_name'] = $content_name;

        return $this;
    }

    /**
     * Sets content_category
     * @param string $content_category The category of the content associated with the event. Example: 'grocery'
     * @return $this
     */
    public function setContentCategory($content_category)
    {
        $this->container['content_category'] = $content_category;

        return $this;
    }

    /**
     * Sets content_ids
     * @param string[] $content_ids The content IDs associated with the event,
     *      such as product SKUs for items in an AddToCart event: ['ABC123', 'XYZ789'].
     *      If content_type is a product, then your content IDs must be an array with a single string value.
     *      Otherwise, this array can contain any number of string values.
     * @return $this
     */
    public function setContentIds($content_ids)
    {
        $this->container['content_ids'] = $content_ids;

        return $this;
    }

    /**
     * Sets contents
     * @param Content[] $contents A list of Content objects that contain the product IDs associated with
     *          the event plus information about the products.
     * @return $this
     */
    public function setContents($contents)
    {
        $this->container['contents'] = $contents;

        return $this;
    }

    /**
     * Sets content_type
     * @param string $content_type A String equal to either 'product' or 'product_group'.
     *      Set to product if the keys you send content_ids or contents represent products.
     *      Set to product_group if the keys you send in content_ids represent product groups.
     * @return $this
     */
    public function setContentType($content_type)
    {
        $this->container['content_type'] = $content_type;

        return $this;
    }

    /**
     * Sets order_id
     * @param string $order_id The order ID for this transaction as a String.
     * @return $this
     */
    public function setOrderId($order_id)
    {
        $this->container['order_id'] = $order_id;

        return $this;
    }

    /**
     * Sets predicted_ltv
     * @param float $predicted_ltv The predicted lifetime value of a conversion event.
     * @return $this
     */
    public function setPredictedLtv($predicted_ltv)
    {
        $this->container['predicted_ltv'] = $predicted_ltv;

        return $this;
    }

    /**
     * Sets num_items
     * @param string $num_items Use only with InitiateCheckout events.
     *      The number of items that a user tries to buy during checkout.
     * @return $this
     */
    public function setNumItems($num_items)
    {
        $this->container['num_items'] = $num_items;

        return $this;
    }

    /**
     * Sets status
     * @param string $status Use only with CompleteRegistration events.
     *      The status of the registration event.
     * @return $this
     */
    public function setStatus($status)
    {
        $this->container['status'] = $status;

        return $this;
    }

    /**
     * Sets a search query made by a user. Use only with Search events.
     * @param string $search_string Use only with Search events.
     *      A search query made by a user.
     * @return $this
     */
    public function setSearchString($search_string)
    {
        $this->container['search_string'] = $search_string;

        return $this;
    }

    /**
     * Returns true if offset exists. False otherwise.
     * @param integer $offset Offset
     * @return boolean
     */
    public function offsetExists($offset)
    {
        return isset($this->container[$offset]);
    }

    /**
     * Gets offset.
     * @param integer $offset Offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }

    /**
     * Sets value based on offset.
     * @param integer $offset Offset
     * @param mixed $value Value to be set
     * @return void
     */
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    /**
     * Unsets offset.
     * @param integer $offset Offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        unset($this->container[$offset]);
    }

    public function normalize()
    {

        $normalized_payload = array();

        $normalized_payload['value'] = $this->getValue();
        $normalized_payload['currency'] =
            Util::normalize('currency', $this->getCurrency());
        $normalized_payload['content_name'] = $this->getContentName();
        $normalized_payload['content_category'] = $this->getContentCategory();
        $normalized_payload['content_ids'] = $this->getContentIds();
        $normalized_payload['content_type'] = $this->getContentType();
        $normalized_payload['order_id'] = $this->getOrderId();
        $normalized_payload['predicted_ltv'] = $this->getPredictedLtv();
        $normalized_payload['num_items'] = $this->getNumItems();
        $normalized_payload['status'] = $this->getStatus();
        $normalized_payload['search_string'] = $this->getSearchString();

        if (isset($this->container['contents'])) {
            $contents = [];
            foreach ($this->getContents() as $content) {
                array_push($contents, $content->normalize());
            }
            $normalized_payload['contents'] = $contents;
        }

        $normalized_payload = array_filter($normalized_payload);

        return $normalized_payload;

    }

    /**
     * Gets a numeric value associated with this event.
     * This could be a monetary value or a value in some other metric.
     * @return float
     */
    public function getValue()
    {
        return $this->container['value'];
    }

    /**
     * Gets currency for the value specified, if applicable.
     * @return string
     */
    public function getCurrency()
    {
        return $this->container['currency'];
    }

    /**
     * Gets content name of the page or product associated with the event. Example: 'lettuce'.
     * @return string
     */
    public function getContentName()
    {
        return $this->container['content_name'];
    }

    /**
     * Gets the category of the content associated with the event. Example: 'grocery'
     * @return string
     */
    public function getContentCategory()
    {
        return $this->container['content_category'];
    }

    /**
     * Gets the content IDs associated with the event,
     * such as product SKUs for items in an AddToCart event: ['ABC123', 'XYZ789'].
     * @return string[]
     */
    public function getContentIds()
    {
        return $this->container['content_ids'];
    }

    /**
     * Gets a list of Content objects that contain the product IDs associated with
     * the event plus information about the products.
     * @return Content[]
     */
    public function getContents()
    {
        return $this->container['contents'];
    }

    /**
     * Gets content type 'product' or 'product_group'
     * @return string
     */
    public function getContentType()
    {
        return $this->container['content_type'];
    }

    /**
     * Gets the order ID for this transaction as a String.
     * @return string
     */
    public function getOrderId()
    {
        return $this->container['order_id'];
    }

    /**
     * Gets the predicted lifetime value of a conversion event.
     * @return float
     */
    public function getPredictedLtv()
    {
        return $this->container['predicted_ltv'];
    }

    /**
     * Gets the number of items that a user tries to buy during checkout
     * @return string
     */
    public function getNumItems()
    {
        return $this->container['num_items'];
    }

    /**
     * Gets the status of the registration event
     * @return string
     */
    public function getStatus()
    {
        return $this->container['status'];
    }

    /**
     * Gets a search query made by a user.
     * @return string
     */
    public function getSearchString()
    {
        return $this->container['search_string'];
    }

    /**
     * Gets the string presentation of the object
     * @return string
     */
    public function __toString()
    {
        if (defined('JSON_PRETTY_PRINT')) { // use JSON pretty print
            return json_encode($this, JSON_PRETTY_PRINT);
        }

        return json_encode($this);
    }
}
