<?php

include_once 'Model.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 Short_Code
 *
 */
class Short_Code {

    private static $array_small_alpha = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
    private static $array_cap_alpha = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    private static $array_numeric1 = array('0', '1', '2', '3', '4', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    private static $array_numeric2 = array('0', '1', '2', '3', '4', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    /*
     * indexes
     */
    private static $pointer = array(0, 0, 0, 0); // $array_numeric1, $array_numeric2, $array_small_alpha,$array_cap_alpha

  
    public static function get_unique_short_code($use_old = FALSE) {

        /*
         * get_old_pointer from db
         */
        if (!$use_old) {
            $oldP = Model::get_old_pointer();
            if ($oldP) {
                self::$pointer = explode(',', $oldP);
            }
        }

        /*
         * update pointer
         */
        if (self::$pointer[3] >= (count(self::$array_cap_alpha) - 1)) {
            self::$pointer[3] = 0;

            if (self::$pointer[2] >= (count(self::$array_small_alpha) - 1)) {
                self::$pointer[2] = 0;

                if (self::$pointer[1] >= (count(self::$array_numeric2) - 1)) {
                    self::$pointer[1] = 0;

                    if (self::$pointer[0] >= (count(self::$array_numeric1) - 1)) {
                        self::$pointer[0] = 0;
                                              
                    } else {
                        self::$pointer[0] = self::$pointer[0] + 1;
                    }
                } else {
                    self::$pointer[1] = self::$pointer[1] + 1;
                }
            } else {
                self::$pointer[2] = self::$pointer[2] + 1;
            }
        } else {
            self::$pointer[3] = self::$pointer[3] + 1;
        }



        $out = self::$array_numeric1[self::$pointer[0]];
        $out .= self::$array_small_alpha[self::$pointer[2]];
        $out .= self::$array_numeric2[self::$pointer[1]];
        $out .= self::$array_cap_alpha[self::$pointer[3]];

        if (self::is_valid($out)) {
            /*
             * update pointter to db
             */
            Model::update_pointer(implode(',', self::$pointer));
            Model::add_new_short_code($out);
            return $out;
        } else {
            self::get_unique_short_code(TRUE);
        }
    }

    public static function is_valid($short_code) {
        $total = Model::get_count_short_code($short_code);
        
        if ($total !== FALSE) {
            return $total <= 0;
        }
        return TRUE;
    }

}
