<?php

include_once 'database.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Model
 *
 */
class Model {

    //put your code here

    public static function save_url_link($url, $short_url, $webhook = "") {

        $query = mysqli_query($GLOBALS['db_connection'],"insert into url_links (long_url,short_url,webhook) values('$url','$short_url','$webhook')");
        if ($query) {
            return mysqli_insert_id($GLOBALS['db_connection']);
        }
        return FALSE;
    }

    public static function set_webhook($long_url, $webhook) {
        $query = mysqli_query($GLOBALS['db_connection'],"update url_links set webhook = '" . addslashes($webhook) . "' where BINARY long_url='$long_url'");
        if ($query) {
            return TRUE;
        }
        return NULL;
    }

    public static function get_webhook($short_url) {

        $query = mysqli_query($GLOBALS['db_connection'],"select webhook from url_links where BINARY short_url='$short_url'");
        if ($query && (mysqli_num_rows($query) > 0)) {
            $result = mysqli_fetch_assoc($query);
            return $result['webhook'];
        }

        return "";
    }

    public static function get_long_url($short_url) {


        $query = mysqli_query($GLOBALS['db_connection'],"select long_url from url_links where BINARY short_url='$short_url'");
        //$query = mysql_query("select long_url from url_links where short_url='$short_url'");
        if ($query && (mysqli_num_rows($query) > 0)) {
            $result = mysqli_fetch_assoc($query);

            return $result['long_url'];
        }
        return NULL;
    }

    public static function get_short_url($long_url) {
        $query = mysqli_query($GLOBALS['db_connection'],"select short_url from url_links where BINARY long_url='$long_url'");
        if ($query && (mysqli_num_rows($query) > 0)) {
            $result = mysqli_fetch_assoc($query);

            return $result['short_url'];
        }
        return NULL;
    }

    public static function get_old_pointer() {
        $query = mysqli_query($GLOBALS['db_connection'],"select indexes from pointer");
        if ($query) {
            if ((mysqli_num_rows($query) > 0)) {
                $result = mysqli_fetch_assoc($query);
                return $result['indexes'];
            } else {
                $query = mysqli_query($GLOBALS['db_connection'],"insert into pointer (indexes) values('0,0,0,0')");
                if ($query) {
                    return '0,0,0,0';
                } else {
                    return FALSE;
                }
            }
        } else {
            return FALSE;
        }
    }

    public static function update_pointer($newval) {
        $query = mysqli_query($GLOBALS['db_connection'],"update pointer set indexes = '$newval'");
        if ($query) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public static function get_count_short_code($short_code) {
        $query = mysqli_query($GLOBALS['db_connection'],"select count(1) as total from short_code where BINARY code='$short_code'");
        if ($query) {
            $result = mysqli_fetch_assoc($query);
            return $result['total'];
        } else {
            return FALSE;
        }
    }

    public static function get_count_longURL($long_url) {
        $query = mysqli_query($GLOBALS['db_connection'],"select count(1) as total from url_links where BINARY long_url='$long_url'");
        if ($query) {
            $result = mysqli_fetch_assoc($query);
            return $result['total'];
        } else {
            return 0;
        }
    }

    public static function add_new_short_code($short_code) {
        $query = mysqli_query($GLOBALS['db_connection'],"insert into short_code (code) values('$short_code')");
        if ($query) {
            return TRUE;
        }
        return FALSE;
    }

    public static function expire_older_url_links() {
        $query = mysqli_query($GLOBALS['db_connection'],"DELETE FROM url_links ORDER BY ID LIMIT 1");
        if ($query) {
            return TRUE;
        }
        return FALSE;
    }

    public static function expire_older_short_codes() {
        $query = mysqli_query($GLOBALS['db_connection'],"DELETE FROM short_code ORDER BY ID LIMIT 1");
        if ($query) {
            return TRUE;
        }
        return FALSE;
    }

}
