<?php

    class statistics{
        
        public static function user( $uid , $fromto = '' ){
            
            $UserData = db::$i->row("SELECT 
            `yield`,
            `win`,
            `win_count`,
            `lose`,
            `lose_count`,
            `profit`,
            `profit_win`,
            `profit_lose`,
            `score_global`,
            `score_1`,
            `score_2`,
            `score_3`
            FROM `user` WHERE `id`='".intval($uid)."'");    
            
            //$UserData['pics'] = $UserData['picks'];
            // UPDATE 30.09.2015 Optymalizacja pobierania danych 
            db::$i->q("select count(1) as pickscount from event_selecting where uid='".intval($uid)."' and `result` is not null group by `hash`",true);
           $UserData['picks'] = $UserData['pics'] = db::$i->rows;
            
                        
            if( $UserWins!=0 ) $UserData['win_percent'] = (100/($UserData['win_count']+$UserData['lose_count'])*$UserData['win_count']);
            else $UserData['win_percent'] = 0;
            
            
            return $UserData;
            
        }
        
        public static function events( $thismonth ){
            
            $data = $thismonth;
            
            $days_avg = 0;
            $course_avg = 0;
            $profit_avg = 0;
            $user_wins = 0;
            $user_lose = 0;
            $user_back = 0;
            $profit = 0;
            $profit_win = 0;
            $yield = 0;
                        

            
            // Ilość typów
            $types = count( $data );

            if( $types ){
                
                $course_avg_array = $days_avg_array = $profit_avg_array = array();

                //
                
                foreach( $thismonth as $item ){
                    $ItemsGroups[$item['hash']][] = $item;
                }
                
                // Ilość picks (pogrupowanych)
                $TypesGroup = count( $ItemsGroups );
                
                // New Yield,Profit Calculator
                foreach( $ItemsGroups as $group_key => $group_items ){

                    $CourseMultipler = 1;
                    $Amount = 0;
                    
                    foreach( $group_items as $item ){    
                      
                        $Amount = $item['amount'];
                        $Course = $item['course'];
                        
                        
                        
                        $days_avg_array[] = ((strtotime($item['event_time'])-strtotime($item['crt']))/60/60) ; // Dni: time::dayDiff($item['crt'],$item['event_time']);
                      
                        switch( $item['result'] ){
                            
                            // Wygrana = ( KURS x STAWKA ) - STAWKA,
                            case 1:
                                $CourseMultipler *= $item['course_one'];
                                //$user_wins++;
                            break;
                            
                            // Przegrana = (S*0*(EVENT_COURSE))-S = -S
                            case 2:
                                $CourseMultipler = 0;
                                //$user_lose++;
                            break;
                            
                            // Zwrot
                            case 3:
                                $CourseMultipler *= 1;
                                //$user_back++;
                            break;
                            
                            // null
                            default:
                            
                            break;
                            
                        }
                        
                        // Jeśli nie było zwrotu dodajemy stawkę do puli wszystkich stawek
                        
                    }
                    
                    // Wygrana/Przegrana/Zwrot
                    
                    switch( $CourseMultipler ){
                        
                        // Przegrana
                        case 0:
                            $user_lose++;
                        break;
                        
                        // Zwrot
                        case 1:
                            $user_back++;
                        break;
                        
                        // Wygrana
                        default:
                            $user_wins++;
                        break;
                        
                    }
                    
                    $profit_avg_array[] = $Amount;
                    $course_avg_array[] = $Course;
                    $profit += $Amount;
                    $profit_win += ($Amount * $CourseMultipler) - $Amount;
                }


            $skutecznosc_tmp = 0;
            $skutecznosc_tmp = $user_wins + $user_lose + $user_back;
            if( $skutecznosc_tmp>0 ) $skutecznosc = (100/( $skutecznosc_tmp )) * $user_wins;
            else $skutecznosc = 0;
                         
                // Średnia ilość dni/godzin przed spotkaniem
                $days_avg = intval( array_sum($days_avg_array) / count($days_avg_array) );
                if( $days_avg > 24 ){
                    $tmp_days_1 = $days_avg%24;
                    if( $tmp_days_1>0 ){
                        $days_avg_h = $tmp_days_1;
                        $days_avg = ($days_avg-$tmp_days_1)/24;
                    }
                } else {
                    $days_avg_h = $days_avg;
                    $days_avg = 0;
                }
                
                // Średni kurs
                $course_avg = array_sum($course_avg_array) / count($course_avg_array);
                
                // Średnia stawka
                $profit_avg = array_sum($profit_avg_array) / count($profit_avg_array);
                
                // Yield
                $yield = (($profit_win) / $profit) * 100;
                
                // Procent wygranych
                if( $profit>0 ){
                    $percent = ((100/$profit)*$profit_win)-100;
                } else $percent=0;
            }
                        
            return array(
                'types' => $TypesGroup,
                'types_once' => $types,
                'profit' => $profit,
                'profit_win' => $profit_win,
                'profit_avg' => number_format( $profit_avg , 2 ),
                'percent' => number_format( $percent , 2 ),
                'wins' => $user_wins,
                'lose' => $user_lose,
                'back' => $user_back,
                'yield' => round( $yield ),
                'course_avg' => number_format( $course_avg , 2 ),
                'mostbukmacher' => $mostbukmacher,
                'days_avg' => $days_avg,
                'days_avg_h' => ($days_avg_h ? $days_avg_h : 0),
                'skutecznosc' => number_format( $skutecznosc , 1 )
            );
            
        }   
        
    }

?>