<?php
namespace App\Model;

use \DateTime;

function sort_by_weidth($a, $b){
    if ($a[4] == $b[4]) {return 0;}
    return ($a[4] < $b[4]) ? -1 : 1;
}

function sort_by_name($a, $b){
    return strcmp($a[0], $b[0]);
}

enum Status: String {
    case New = "new";
    case Active = "active";
    case Done = "done";
}

class Session {
    public string $uuid;
    public string $name;
    public string $media;
    public string $anonce;
    public int $parralell;
    public int $repeate;
    public int $pause;
    public string $status;
    private array $calls;
    public array $meta = [];
    public string $operator;
    public $createTime;
    public $startTime;
    public $completeTime;
    public array $numbers = [];
    public array $actions;
    public string $from_user;
    public string $number_for_record;
    public string $pattern;
    public string $owner;

    public function __construct($name){
	/* $name = explode("_", $name);
	   $this->name = implode(" ", $name); */
	$this->name = $name;
	$this->calls = [];
    }

    public function add_subscriber(array $user){
	if($user[2] == null){
	    throw new Exception\EmptyNumberException($user[0], 100);
	}
	foreach($this->meta as $u){
	    if($u[0] == $user[0]){
		throw new Exception\UserExistException($user[0], 101);
	    }
	}
	$this->meta[] = $user;
	usort($this->meta, "App\Model\sort_by_name");
	usort($this->meta, "App\Model\sort_by_weidth");
	$this->numbers = [];
	foreach($this->meta as $u){
	    $this->numbers[] = $u[2];
	}
    }

    public function remove_subscriber($name){
	$temp = [];
	foreach($this->meta as $u){
	    if($u[0] != $name){
		$temp[] = $u;
		$this->numbers[] = $u[2];
	    }
	}
	$this->meta = $temp;
    }

    public function setMedia($file){
	/* $parts = pathinfo($file);
	   $this->media = $parts['basename']; */
	$this->media = $file;
    }

    public function setAnonce($file){
	/* $parts = pathinfo($file);
	   $this->anonce = $parts['basename']; */
	$this->anonce = $file;
    }

    public function getAnonce(){
	$parts = pathinfo($this->anonce);
	return $parts['basename'];
    }
    
    public function getMedia(){
	$parts = pathinfo($this->media);
	if ($parts['dirname'] == "/tmp") {
	    return "по сесии";
	}
	
	return $parts['basename'];
    }
    
    public function setCall(Call $call){
	$this->calls[] = $call;
    }

    public function getCalls(){
	return $this->calls;
    }

    public function success(){
	$temp = [];;
	foreach($this->calls as $call){
	    if($call->status == 'dtmf'){
		$temp[] = $call;
	    }
	}
	$success = [];
	foreach($temp as $call){
	    foreach($this->meta as $m){
		if($call->number == $m[2]){
		    $call->user = $m;
		    $success[] = $call;
		}
	    }
	}
	return $success;
    }
    
    public function active(){
	$temp = [];
	foreach($this->calls as $call){
	    if($call->status == 'active' or $call->status == 'play'
		or $call->status == 'pause' or $call->status == 'init'){
		$temp[] = $call;
	    }
	}
	$active = [];
	foreach($temp as $c){
	    foreach($this->meta as $m){
		if($c->number == $m[2]){
		    $c->user = $m;
		    $active[] = $c;
		}
	    }
	}
	return $active;
    }

    public function wait(){
	$temp = [];
	foreach($this->calls as $call){
	    if($call->status == 'wait' or $call->status == 'в очереди'){
		$temp[] = $call;
	    }
	}
	$wait = [];
	foreach($temp as $call){
	    foreach($this->meta as $m){
		if($call->number == $m[2]){
		    $call->user = $m;
		    $wait[] = $call;
		}
	    }
	}
	return $wait;
    }
    
    public function fail(){
	$temp = [];
	foreach($this->calls as $call){
	    if($call->status == 'fail' or $call->status == 'close'){
		$temp[] = $call;
	    }
	}
	$fail = [];
	foreach($temp as $call){
	    foreach($this->meta as $m){
		if($call->number == $m[2]){
		    $call->user = $m;
		    $fail[] = $call;
		}
	    }
	}
	return $fail;
    }

    public function add_call(Call $call){
	$this->calls[] = $call;
    }

    public function is_check(){
	$checked = [];
	foreach($this->meta as $u){
	    $checked[$u[0]] = true;
	}
	return $checked;
    }


    public function for_report(){
	$temp_users = [];
	/* echo var_dump($session['data'][0]['meta']); */
	foreach($this->meta as $u){
	    /* foreach ($u as $el){ */
	    $temp_users[$u[3]][] = $u;
	    /* } */
	};
	
	$done = $this->calls;
	/* foreach ($done as $n => $key ){
	   $done[$n][0] = $key[0];
	   $done[$n][1] = explode(" ",$key[1])[0];
	   $done[$n][2] = explode(" ",$key[2])[0];
	   $done[$n][3] = explode(" ",$key[4])[0];
	   } */

	/* $temp = $session['data'][0]['meta']; */
	/* echo var_dump($done); */
	$meta = [];
	foreach ($temp_users as $group_name => $val){
	    /* echo var_dump($val); */
	    $users = [];
	    /* echo var_dump($group_name); */
	    foreach($val as $el){
		$mobile = $el[2];
		$pattern = '/80(\d{9})/';
		$replacement = '+375\1';
		$mobile = preg_replace($pattern, $replacement, $mobile);
		$pattern = '/(\d{5})\d{4}(\d{2})/';
		$replacement = '\1xxxx\2';
		$newMobile = preg_replace($pattern, $replacement, $mobile);
		$call = $this->get_call_status($el[2]);
		if( $call->status == "dtmf"){
		    $el[4] = $call->status;
		} else {
		    $el[4] = $call->meta;
		}
		$el[] = $call->get_close_time();
		$el[] = $call->attempt;
		$el[2] = $newMobile; 
		$users[] = $el;

	    }
	    $meta[$group_name] = $users;
	}
	return $meta;
    }

    private function get_call_status($number){
	foreach($this->calls as $call){
	    if($call->number == $number){
		return $call;
	    }
	}
    }

    public function duration(){
	$format = 'H:i:s d.m.Y';
	$dateTime = DateTime::createFromFormat($format,$this->startTime);
	if ($this->compliteTime){
	    $compliteTime = DateTime::createFromFormat($format,$this->compliteTime);
	    $duration = $compliteTime->diff($dateTime);
	    
	} else {
	    $now = new DateTime();
	    $duration = $now->diff($dateTime);
	}
	return $duration->format('%I:%S');
    }
}
