<?php

class whatsApp
{
    private $base = 'https://api.taqnyat.sa/wa/v2/messages/';
    private $method = 'POST';
    private $header = array();
    public function __construct($token)
    {
        $this->header = array(
            'Content-Type: application/json',
            'Authorization: Bearer ' . $token
        );
    }

    public function text($to, $message)

    {
        $data = '{
            "to": "' . $to . '",
            "type": "text",
            "text": 
                {
                    "body": "' . $message . '"
                }
            }';
        $response = API($this->base, $this->method, $data, $this->header);

        return $response;
    }

    public function button($to, $message, $buttons = [], $footer = '')
    {
        $data = '{
            "to": "' . $to . '",
            "type": "interactive",
            "interactive": {
                "type": "button",
                "body": {
                    "text": "' . $message . '"
                },
                "footer": {
                    "text": "' . $footer . '"
                },
                "action": {
                    "buttons": [
                    ]
                }
            }
        }';

        $data = json_decode($data, true);
        $data['interactive']['action']['buttons'] = $buttons;
        $data = json_encode($data);
        $response = API($this->base, $this->method, $data, $this->header);
        return $response;
    }

    public function wa_list($to, $message, $buttons = [], $footer = '', $title = '')
    {

        $data = '{
            "to": "' . $to . '",
            "type": "interactive",
            "interactive": {
                "type": "list",
                "body": {
                    "text": "' . $message . '"
                },
                "footer": {
                    "text": "' . $footer . '"
                },
                "action": {
                    "button":"' . $title . '",
                    "sections": [
                        {
                            "rows": []
                        }
                    ]
                }
            }
        }';
        $data = json_decode($data, true);
        $data['interactive']['action']['sections'][0]['rows'] = $buttons;
        $data = json_encode($data);
        $response = API($this->base, $this->method, $data, $this->header);

        return $response;
    }
    public function image($to, $image)
    {
        $data = '{
            "to": "' . $to . '",
            "type": "image",
            "image": {
                "link":"' . $image . '"
            }
        }';

        $response = API($this->base, $this->method, $data, $this->header);
        return $response;
    }
}
