<?php
class api
{

	public function balance($smart, $api, $api_url, $api_key)
	{
		if ($smart == 1) {
			$url = $api_url . "?key=" . $api_key . "&action=balance";
			$result = curl_get($url);
			if ($result !== 'false') {
				$res = strtolower($result);
				$res = json_decode($res, 1);
				if (!isset($res['error'])) {

					$b = number_format($res['balance'], 2)?:'مشکل در دریافت';
					return ['status' => 'OK','balance'=>$b];
				} else {
					return ['status' => 'false','balance'=>'دریافت نشد'];
				}
			} else {
				return ['status' => 'false'];
			}
		}
	}
	//---------------------------------------//
	public function status($smart, $api, $api_url, $api_key, $id)
	{
		if ($smart == 1) {
			$url = $api_url . "?key=" . $api_key . "&action=status&order=" . $id;
			$result = curl_get($url);
			if ($result !== 'false') {
				$res = strtolower($result);
				$res = json_decode($res, 1);
				if (!isset($res['error'])) {
					return $res;
				} else {
					return ['status' => 'false', 'error' => $res['error']];
				}
			} else {
				return ['status' => 'false'];
			}
		}
	}
	//---------------------------------------//
	public function add_order($smart, $api, $api_url, $api_key, $id, $link, $quantity)
	{
		if ($smart == 1) {
			$url = $api_url . "?key=" . $api_key . "&action=add&service=" . $id . "&link=" . $link . "&quantity=" . $quantity;
			$result = curl_get($url);
			if ($result !== 'false') {
				$res = strtolower($result);
				$res = json_decode($res, 1);
				if (!isset($res['error'])) {
					return ['status' => 'OK', 'code' => $res['order']];
				} else {
					return ['status' => 'false', 'error' => $res['error']];
				}
			} else {
				return ['status' => 'false'];
			}
		}
	}
	//---------------------------------------//
	public function services($smart, $api, $api_url, $api_key)
	{
		if ($smart == 1) {
			$url = $api_url . "?key=" . $api_key . "&action=services";
			$result = curl_get($url);
			if ($result !== 'false') {
				$res = json_decode($result, 1);
				if (!isset($res['error'])) {
					return $res;
				} else {
					return ['status' => 'false'];
				}
			} else {
				return ['status' => 'false'];
			}
		}
	}
}
