<?php

/**
 * @copyright 0arkcode
 * @author 0arkcode@gmail.com
 * @license 0arkcode
 */

    class statement{

        public static $i;
        public static $default_text = array(
            'update'=> array(
                'type' => 'success',
                'text' => 'Dane zostały zaktualizowane'
            ),
            'delete' => array(
                'type' => 'success',
                'text' => 'Dane zostały usunięte'
            ),
            'save' => array(
                'type' => 'success',
                'text' => 'Dane zostały zapisane'
            ),
            'mysql' => array(
                'type' => 'danger',
                'text' => 'Coś poszło źle... Spróbuj ponownie za chwilę.'
            ),
            'empty' => array(
                'type' => 'danger',
                'text' => 'Brak elementów do wyświetlenia'
            ),
            'noexists' => array(
                'type' => 'danger',
                'text' => 'Nie znaleziono elementu o takim identyfikatorze'
            ),
            'unknown' => array(
                'type' => 'danger',
                'text' => 'Coś poszło źle... Spróbuj ponownie za chwilę.'
            ),
            'error_name' => array(
                'type' => 'danger',
                'text' => 'Niepoprawna nazwa'
            ),
            'error_url' => array(
                'type' => 'danger',
                'text' => 'Niepoprawny adres URL'
            ),
            'error_server' => array(
                'type' => 'danger',
                'text' => 'Wystąpił błąd podczas rejestracji, możliwe że nasze serwery są obecnie przeciążone. Spróbuj ponownie za chwilę'
            )
        );
           
        public $LastStatus = false;     
        public $html = array();
        public $point = 'default';
        public $error_count = 0;
        public $display_info = false;
        public $display_error = false;
        public $LastHtml = '';
        
        public function __construct(){
            $html = array(
                'default' => ''
            );
            
            self::$i = $this;
        }
        
        /**
         * Ustaw status na stronie
         * @param mixed $text
         * @param string $type - primary, success, info, warning, danger
         * @param string $point - klucz w tablicy
         */
        public function set( $text , $type = 'primary' , $point = false , $params = array('html' => false,'align_class' => '') ){
            
            if( !isset($params['html']) ) $params['html'] = false;
            if( !isset($params['align_class']) ) $params['align_class'] = '';
            else $params['align_class'] = ' ' . $params['align_class'];
            
            $this->LastStatus = $type;
            
            switch( $this->LastStatus ){
                
                case 'primary':
                case 'success':
                    $this->display_info = true;
                break;
                
                case 'danger':
                    $this->display_error = true;
                break;
            }
            
            if( array_key_exists( $text , self::$default_text ) )
            {
                $this->LastStatus = self::$default_text[$text]['type'];
                $text = $this->LastHtml = self::$default_text[$text]['text'];
                
                if( $this->LastStatus == 'danger' ) $this->error_count++;
            } 
            else 
            {
                $this->LastHtml = $text;
            }
  
        
            if( $params['html'] ) $this->html[($point ? $point : $this->point)] = '<div class="p10 bg-'.$this->LastStatus.$params['align_class'].'">'.($text).'</div>';
            else $this->html[($point ? $point : $this->point)] = '<div class="p10 bg-'.$this->LastStatus.$params['align_class'].'">'.l($text).'</div>';

        }
        
        
        public static function danger( $text )
        {
            self::$i->set( $text , 'danger' );
        }

        public static function success( $text )
        {
            self::$i->set( $text , 'success' );
        }
                
        public function setPoint( $name ){
            $this->point = $name;
        }
        
        public static function JsDisplay()
        {
            if( !empty( self::$i->LastHtml ) )
            {
                if( !stristr( self::$i->LastHtml  , '<strong>' ) )
                {
                    $Content = ( self::$i->LastStatus == 'danger' ? ( "<strong>" . l('Błąd') . "</strong>" ) : ( "<strong>" . l('Zrobione!') . "</strong>" ) ) . self::$i->LastHtml;
                } 
                else 
                {
                    $Content = self::$i->LastHtml;
                }
    
                return js::jqueryCode( "AlertWindow('" . $Content . "','".self::$i->LastStatus."');" );
            }

        }
        
        public static function display( $point = false ){
            return self::$i->html[($point ? $point : self::$i->point)];
        }
        
        /**
         * @param mixed $point
         * @return bool
         */
        public static function is_displayed( $point = false ){
            if( 
                isset( self::$i->html[($point ? $point : self::$i->point)] ) AND 
                !empty( self::$i->html[($point ? $point : self::$i->point)] ) )
            {
                 return true;
            }
            
            return false;
        }
        
        public function is_valid(){
            if( !$this->display_error ) return true;
            else return false;
        }
    }

?>