<?php
    class BlitzScriptAPI {
        public $version = "0.1";
        public function __construct ($login, $password)  {
            error_reporting(E_ALL);
            $this->domain = $_SERVER['SERVER_NAME'];
            $this->params = [];
            $this->params["api_login"]      = $login;
            $this->params["api_password"]   = $password;

            $this->url = "http://hyip/api/".$this->version."/index.php";
        }

        public function post_json_request($url, $data=[]) {
            $postdata = http_build_query($data);
            $opts = array('http' =>
                array(
                    'method'  => 'POST',
                    'header'  => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postdata
                )
            );
            $context  = stream_context_create($opts);
            return json_decode(file_get_contents($url, false, $context), true);
        }

        private function query($url, $data=[]) {
            $result = $this->post_json_request($url, $data + $this->params);
            if ($result === false) {
                return [
                    "error"     => true,
                    "message"   => "Ошибка запроса к сервису!",
                ];
            }
            return $result;
        }

        public function api_login_balance() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_balance",
            ]);
        }

        public function api_auth_login() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_balance",
            ]);
        }

        public function api_signup($login, $email, $password, $password2) {
            $fields = ['login', 'password', 'password2', 'email', ];
            return $this->query($this->url, [
                "func" => "api_signup",
            ] + compact($fields));
        }

        public function api_login_send_answer_ticket($id, $text) {
            $fields = ['id', 'text', ];
            return $this->query($this->url, [
                "func" => "api_login_send_answer_ticket",
            ] + compact($fields));
        }

        public function api_login_new_ticket($subject, $text) {
            $fields = ['subject', 'text', ];
            return $this->query($this->url, [
                "func" => "api_login_new_ticket",
            ] + compact($fields));
        }

        public function api_login_review ($img, $text) {
            $fields = ['img', 'text', ];
            return $this->query($this->url, [
                "func" => "api_login_review",
            ] + compact($fields));
        }

        public function api_support ($message) {
            $fields = ['message', ];
            return $this->query($this->url, [
                "func" => "api_support",
            ] + compact($fields));
        }

        public function api_get_content ($type) {
            $fields = ['type', ];
            return $this->query($this->url, [
                "func" => "api_get_content",
            ] + compact($fields));
        }

        public function api_login_save_profile ($name = "", $lastname = "", $phone = "", $country = "", $vk = "", $city = "", $skype = "", $reset_password = "", $notification_auth_to_email = "") {
            $fields = ['name', 'lastname', 'phone', 'country', 'vk', 'city', 'skype', 'reset_password', 'notification_auth_to_email', ];
            return $this->query($this->url, [
                "func" => "api_login_save_profile",
            ] + compact($fields));
        }

        public function api_login_create_payment ($amount, $system="") {
            $fields = ['amount', 'system', ];
            return $this->query($this->url, [
                "func" => "api_login_create_payment",
            ] + compact($fields));
        }

        public function api_login_create_deposit ($amount, $plan="", $system="") {
            $fields = ['amount', 'plan', 'system', ];
            return $this->query($this->url, [
                "func" => "api_login_create_deposit",
            ] + compact($fields));
        }

        public function api_login_save_password ($last_password, $password, $password2) {
            $fields = ['last_password', 'password', 'password2', ];
            return $this->query($this->url, [
                "func" => "api_login_save_password",
            ] + compact($fields));
        }

        public function api_login_create_invest ($system, $summa, $plan="") {
            $fields = ['system', 'summa', 'plan', ];
            return $this->query($this->url, [
                "func" => "api_login_create_invest",
            ] + compact($fields));
        }

        public function api_login_save_wallets ($payment_system=[]) {
            return $this->query($this->url, [
                "func" => "api_login_save_wallets",
            ] + $payment_system);
        }

        public function api_login_info() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_info",
            ]);
        }

        public function api_ps_info() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_ps_info",
            ]);
        }

        public function api_plans_info() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_plans_info",
            ]);
        }

        public function api_login_list_deposits() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_list_deposits",
            ]);
        }

        public function api_login_list_invest() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_list_invest",
            ]);
        }

        public function api_login_list_payment() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_list_payment",
            ]);
        }

        public function api_currency() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_currency",
            ]);
        }

        public function api_login_deposit() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_deposit",
            ]);
        }

        public function api_login_invest() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_invest",
            ]);
        }

        public function api_login_payment() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_payment",
            ]);
        }

        public function api_login_refer() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_refer",
            ]);
        }

        public function api_login_profit() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_login_profit",
            ]);
        }

        public function api_show_invest() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_show_invest",
            ]);
        }

        public function api_show_deposit() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_show_deposit",
            ]);
        }

        public function api_show_payment() {
            $fields = [];
            return $this->query($this->url, [
                "func" => "api_show_payment",
            ]);
        }

        public function alert($message) { ?>
            <script>window.alert("<?php echo $message; ?>");</script>
        <?php
        }

        public function format_currency($money, $currency) {
            return strtolower($currency) === "btc" ? format_btc($money) : format_money($money);
        }

        public function format_btc($money) {
            return sprintf("%01.8f", $money);
        }

        public function format_money($money) {
            return sprintf("%01.2f", $money);
        }

        public function post($k, $v=false) {
            return $v ? $_POST[$k] = $v : isset($_POST[(string)$k]) ? $_POST[(string)$k] : "";
        }

        public function get($k, $v=false) {
            return $v ? $_GET[$k] = $v : isset($_GET[(string)$k]) ? $_GET[(string)$k] : "";
        }

        public function request($k, $v=false) {
            return $v ? $_REQUEST[$k] = $v : isset($_REQUEST[(string)$k]) ? $_REQUEST[(string)$k] : "";
        }
    }
