<?php
namespace PEZESHKYAR\SMS\Drivers;

class Melipayamak{
    const PATH = "https://rest.payamak-panel.com/api/SendSMS/%s";

	const WSDL_URL = "http://api.payamak-panel.com/post/Send.asmx";
	const SOAP_ACTION = "http://tempuri.org/SendByBaseNumber";
    
    protected $username;
	
	protected $password;


	public function __construct($username,$password)
	{
		$this->username = $username;
		$this->password = $password;
	}

    protected function getPath($path,$method)
	{
		return sprintf($path, $method);
	}

    protected function execute($url, $data = null){
		$fields_string = "";
		
		if (!is_null($data)) {
			
			$fields_string = http_build_query($data);
			
		}
		
		$handle = curl_init();
		
		curl_setopt($handle, CURLOPT_URL, $url);
		
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
		
		curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
		
		curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
		
		curl_setopt($handle, CURLOPT_POST, true);
		
		curl_setopt($handle, CURLOPT_POSTFIELDS, $fields_string);
		
		
		$response     = curl_exec($handle);
		
		$code         = curl_getinfo($handle, CURLINFO_HTTP_CODE);
		
		$curl_errno   = curl_errno($handle);
		
		$curl_error   = curl_error($handle);
		
		if ($curl_errno) {
			
			throw new \Exception($curl_error);
			
		}
		
		curl_close($handle);

		return $response;
    }

	protected function executeSoap($xml)
	{
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, self::WSDL_URL);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
		curl_setopt($ch, CURLOPT_HTTPHEADER, [
			"Content-Type: text/xml; charset=utf-8",
			"SOAPAction: \"" . self::SOAP_ACTION . "\"",
			"Content-Length: " . strlen($xml)
		]);

		$response = curl_exec($ch);

		if (curl_errno($ch)) {
			throw new \Exception('SOAP Error: ' . curl_error($ch));
		}

		curl_close($ch);

		// استخراج نتیجه از XML
		if (preg_match('/<SendByBaseNumberResult>(.*?)<\/SendByBaseNumberResult>/', $response, $matches)) {
			return $matches[1];
		}

		return $response; // fallback: raw XML if pattern fails
	}

    public function send($to,$from,$text,$isFlash=false)
	{
		
		$url = $this->getPath(self::PATH,'SendSMS');
		
		$data = [
		'username' => $this->username,
		'password' => $this->password,
		'to' => $to,
		'from' => $from,
		'text' => $text,
		'isflash' => $isFlash
		];
		
		return $this->execute($url,$data);
		
	}
	
	public function sendByBaseNumber(array $text, $to, $bodyId)
	{
		$textXml = '';
		foreach ($text as $item) {
			$textXml .= "<string>{$item}</string>";
		}

		$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendByBaseNumber xmlns="http://tempuri.org/">
      <username>{$this->username}</username>
      <password>{$this->password}</password>
      <text>{$textXml}</text>
      <to>{$to}</to>
      <bodyId>{$bodyId}</bodyId>
    </SendByBaseNumber>
  </soap:Body>
</soap:Envelope>
XML;

		return $this->executeSoap($xml);
	}
}

