<?php

class AHACHAT_WP_API
{
    function __construct()
    {
        $this->fb2 = new AhaChatFacebookApi();
    }

    public function getAppId()
    {
        return get_option('ahachat_app_id');
    }

    public function AHA_Get_FanPage()
    {
        return get_option('ahachat_page_name');
    }

    public function AHA_Get_Access_Token_Page()
    {
        return ['access_token' => get_option('ahachat_broadcast_token')];
    }

    // SEND MESSENGER WITH TEXT
    public function AHA_Send_message_text_user_ref($page_id, $page_token, $content_message, $user_ref)
    {
        $Data = [
            "recipient" => array("user_ref" => $user_ref),
            "message" => array("text" => $content_message)
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response["message_id"];
    }

    // SEND MESSENGER WITH LIST TEMPLATE
    public function AHA_Send_message_list_template_user_ref($page_id, $page_token, $user_ref, $array_product, $url_button_generic, $title_button_generic)
    {
        $Data = [
            "recipient" => array("user_ref" => $user_ref),
            "message" => array(
                "attachment" => array(
                    "type" => "template",
                    "payload" => array(
                        "template_type" => "list",
                        "top_element_style" => "large",
                        "elements" => $array_product,
                        "buttons" => array(
                            array(
                                "type" => "web_url",
                                "url" => $url_button_generic,
                                "title" => $title_button_generic
                            )
                        )
                    )
                )
            )
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response;//$graphNode['result'];
    }

    // SEND MESSENGER WITH ONE PRODUCT
    public function AHA_Send_message_one_product_user_ref($page_id, $page_token, $user_ref, $array_product)
    {
        $Data = [
            "recipient" => array("user_ref" => $user_ref),
            "message" => array(
                "attachment" => array(
                    "type" => "template",
                    "payload" => array(
                        "template_type" => "generic",
                        "image_aspect_ratio" => "square",
                        "elements" => $array_product,
                    )
                )
            )
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response;//$graphNode['result'];
    }

    // GET INFO USER
    public function AHA_Get_Info_User_With_Recepit_ID($page_token, $recipient_id)
    {
        try {
            $response = $this->fb2->request("GET", "$recipient_id?fields=first_name,last_name,profile_pic,locale,gender", [], $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }

        return $response;
    }

    // SEND MESSENGER WITH USER ADD TO CART
    public function AHA_Send_message_user_add_cart($page_id, $page_token, $content_message, $recipient_id)
    {
        $Data = [
            "recipient" => array("id" => $recipient_id),
            "message" => array("text" => $content_message)
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request('POST', "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response["recipient_id"];//$graphNode['result'];
    }

    // SEND MESSENGER WITH USER ADD TO CART WITH MESSAGE TAG
    public function AHA_Send_message_user_add_cart_WITH_TAG($page_id, $page_token, $content_message, $recipient_id, $message_tag)
    {
        $Data = [
            "recipient" => array("id" => $recipient_id),
            "message" => array("text" => $content_message),
            "messaging_type" => "MESSAGE_TAG",
            "tag" => $message_tag
        ];
        if ($message_tag == "STANDARD_MESSAGING") {
            unset($Data["messaging_type"]);
            unset($Data["tag"]);
        }
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request('POST', "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response["recipient_id"];//$graphNode['result'];
    }

    // SEND MESSAGE WITH TEXT BUTTON
    public function AHA_SEND_TEXT_BUTTON($page_id, $page_token, $text_message, $user_ref, $sender_id, $array_button)
    {
        $recipient_id = array();
        if (empty($user_ref)) {
            $recipient_id = array("id" => $sender_id);
        } else {
            $recipient_id = array("user_ref" => $user_ref);
        }
        $Data = [
            "recipient" => $recipient_id,
            "message" => array("attachment" => array(
                "type" => "template",
                "payload" => array(
                    "template_type" => "button",
                    "text" => $text_message,
                    "buttons" => $array_button

                )

            )
            )
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response["message_id"];//$graphNode['result'];
    }

    //
    // SEND MESSAGE WITH IMAGE
    // Upload Image
    public function AHA_UPLOAD_IMAGE_CAMPAIGN($page_token, $url_image)
    {
        $Data = [
            "message" => array(
                "attachment" => array(
                    "type" => "image",
                    "payload" => array(
                        "url" => $url_image,
                        "is_reusable" => true,
                    )
                )
            )
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "/me/message_attachments", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response["attachment_id"];//$graphNode['result'];
    }

    // SEND MESSAGE WITH IMAGE
    public function AHA_SEND_IMAGE_CAMPAIGN($page_id, $page_token, $url_image, $user_ref, $sender_id)
    {
        $recipient_id = array();
        if (empty($user_ref)) {
            $recipient_id = array("id" => $sender_id);
        } else {
            $recipient_id = array("user_ref" => $user_ref);
        }
        $Data = [
            "recipient" => $recipient_id,
            "message" => array(
                "attachment" => array(
                    "type" => "image",
                    "payload" => array(
                        "url" => $url_image,
                        "is_reusable" => true,
                    )
                )
            )
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response["message_id"];//$graphNode['result'];
    }

    public function AHA_SEND_IMAGE_BUTTON_CAMPAIGN($page_id, $page_token, $user_ref, $sender_id, $attachment_id, $array_button)
    {
        $recipient_id = array();
        if (empty($user_ref)) {
            $recipient_id = array("id" => $sender_id);
        } else {
            $recipient_id = array("user_ref" => $user_ref);
        }
        $Data = [
            "recipient" => $recipient_id,
            "message" => array(
                "attachment" => array(
                    "type" => "template",
                    "payload" => array(
                        "template_type" => "media",
                        "elements" => array(
                            array(

                                "media_type" => "image",
                                "attachment_id" => $attachment_id,
                                "buttons" => $array_button
                            )

                        )


                    )
                )
            )
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response["message_id"];//$graphNode['result'];
    }

    //

    // SEND MESSENGER WITH LIST TEMPLATE
    public function AHA_Send_Order_Template_by_user_ref($page_id, $page_token, $user_ref, $recipient_name, $order_number, $currency, $payment_method, $order_url, $array_address, $array_sumary, $array_coupon, $timestamp, $array_product)
    {
        $Data = [
            "messaging_type" => NON_PROMOTIONAL_SUBSCRIPTION,
            "recipient" => array("user_ref" => $user_ref),
            "message" => array(
                "attachment" => array(
                    "type" => "template",
                    "payload" => array(
                        "template_type" => "receipt",
                        "sharable" => true,
                        "recipient_name" => $recipient_name,
                        "order_number" => $order_number,
                        "currency" => $currency,//"USD",
                        "payment_method" => $payment_method,//"Visa 2345",
                        "order_url" => $order_url,
                        "timestamp" => $timestamp,
                        "address" => $array_address,
                        "summary" => $array_sumary,
                        "adjustments" => $array_coupon,
                        //  "elements" => $array_product,
                        "elements" => $array_product,


                    )
                )
            )
        ];
        try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $this->fb2->request("POST", "$page_id/messages", $Data, $page_token);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return $response;//$graphNode['result'];
    }
}

?>