<?php

namespace cab\system;

class i_ws_update_content
{
    private static $arr_update=[]  ;
    private static $arr_append=[]  ;
    private static $arr_prepend=[]  ;
    private static $arr_reload=[]  ;

    public static function update_content($group_id,$member_id,$id,$value)
    {
        //echo 'update: '.$id.', value='.$value.'<br>' ;
        static::$arr_update[$group_id][$member_id][$id]=$value ;

    }

    public static function append_content($group_id,$member_id,$id,$value)
    {
        //echo 'append: '.$id.', value='.$value.'<br>' ;
        static::$arr_append[$group_id][$member_id][$id]=$value ;
    }

    public static function prepend_content($group_id,$member_id,$id,$value)
    {
        //echo 'append: '.$id.', value='.$value.'<br>' ;
        static::$arr_prepend[$group_id][$member_id][$id]=$value ;
    }

    public static function reload_page($group_id,$member_id,$url)
    {
        //echo 'append: '.$id.', value='.$value.'<br>' ;
        static::$arr_reload[$group_id][$member_id][$url]=1 ;
    }

    public static function send($session_uid='')
    {  //ob_start() ;
       //damp_array(static::$arr_update);
       //damp_array(static::$arr_append);
       $message_by_groups=[] ; $result=[] ;


       if (sizeof(static::$arr_update)) foreach (static::$arr_update as $group_id=>$content_to_group)
       {  if (sizeof($content_to_group)) foreach ($content_to_group as $account_id=>$arr_content)
            if (sizeof($arr_content)) foreach ($arr_content as $id=>$value)
                 $message_by_groups[$group_id][]=['account_id'=>($account_id)?:0,'element_id'=>$id,'value'=>$value,'mode'=>'update','session_uid'=>$session_uid] ;
       }
       if (sizeof(static::$arr_append)) foreach (static::$arr_append as $group_id=>$content_to_group)
       {  if (sizeof($content_to_group)) foreach ($content_to_group as $account_id=>$arr_content)
            if (sizeof($arr_content)) foreach ($arr_content as $id=>$value)
                $message_by_groups[$group_id][]=['account_id'=>($account_id)?:0,'element_id'=>$id,'value'=>$value,'mode'=>'append','session_uid'=>$session_uid] ;
       }
       if (sizeof(static::$arr_prepend)) foreach (static::$arr_prepend as $group_id=>$content_to_group)
       {  if (sizeof($content_to_group)) foreach ($content_to_group as $account_id=>$arr_content)
            if (sizeof($arr_content)) foreach ($arr_content as $id=>$value)
                $message_by_groups[$group_id][]=['account_id'=>($account_id)?:0,'element_id'=>$id,'value'=>$value,'mode'=>'prepend','session_uid'=>$session_uid] ;
       }

       if (sizeof(static::$arr_reload)) foreach (static::$arr_reload as $group_id=>$content_to_group)
       {  if (sizeof($content_to_group)) foreach ($content_to_group as $account_id=>$arr_content)
            if (sizeof($arr_content)) foreach ($arr_content as $url=>$value)
                $message_by_groups[$group_id][]=['account_id'=>($account_id)?:0,'url'=>_PATH_TO_SITE.$url,'mode'=>'reload_page','session_uid'=>$session_uid] ;
       }

        static::$arr_prepend=[] ;
        static::$arr_append=[] ;
        static::$arr_update=[] ;
        static::$arr_reload=[] ;

       //damp_array($message_by_groups);

        // собираем сообщен
       if (sizeof($message_by_groups)) foreach ($message_by_groups as $group_id=>$arr_content)
       {
           $message=['type'=>'update_content',
                     'content'=>$arr_content
                    ] ;

           $result= \cab\system\i_ws::send_mess_to_WS($group_id,$message);
           //damp_array($result);
       }
       //$text=ob_get_clean() ;
       //LOGS()->reg_log('send', $text) ;
       return($result) ;
    }
}
