<?php

class PlDateInputConverter {
    
    private $userInput;
    private $originalUserInput;
    
    public function __construct($userInput = "")
    {
        $this->userInput = $userInput;
        $this->originalUserInput = $userInput;
    }
    
    public function toDate()
    {
        if (!preg_match("/[\.\/-]/", $this->userInput)) {
            return $this->originalUserInput; 
        }
        
        $format = $this->getDateFormatFromInput();
        
        $date = PhonalisaDate::newServerDate($this->userInput, $format);
        //$date = DateTime::createFromFormat($format, $this->userInput);
        if ($date) {
            $searchDate = PhonalisaDate::convertServerDateTo('Y-m-d\TH:i:s', $date, false);
            //$searchDate = $date->format('Y-m-d\TH:i:s');
            $searchDate = substr($searchDate, 0, strlen($this->originalUserInput));
            return $searchDate;
        }
        return $this->originalUserInput;
    }
    
    public function getDateFormatFromInput()
    {
        $output = $this->userInput;
        
        if (preg_match("/^[\d]{4}/", $this->userInput)) {
            $dateDelimiter = "-";
            $output = preg_replace("/^[\d]{4}/", "Y", $output, 1);
            if (preg_match("/-[\d]{1}$/", $output)) {
                $this->userInput = $this->userInput."1";
                $output = $output ."1";
            }
            $output = preg_replace("/[\d]{2}/", "m", $output, 1);
            $output = preg_replace("/[\d]{2}/", "d", $output, 1);
            
        } else if (preg_match("/^[\d]{2}\./", $this->userInput)) {
            $dateDelimiter = "\.";
            $output = preg_replace("/[\d]{4}/", "Y", $output);
            $matches = array();
            preg_match_all("/[\d]{2}/", $output, $matches);            
            
            if (count($matches[0]) < 2 ){
                $output = preg_replace("/^[\d]{2}$dateDelimiter/", "m.", $output, 1);
            } else {
                $output = preg_replace("/^[\d]{2}$dateDelimiter/", "d.", $output, 1);
                $output = preg_replace("/[\d]{2}$dateDelimiter/", "m.", $output, 1);
            }
            
            
        } else if (preg_match("/^[\d]{2}\//", $this->userInput)) {
            $dateDelimiter = "\/";
            $output = preg_replace("/[\d]{4}/", "Y", $output);
            $matches = array();
            preg_match_all("/[\d]{2}/", $output, $matches);            
            
            if (count($matches[0]) < 2 ){
                $output = preg_replace("/^[\d]{2}$dateDelimiter/", "d/", $output, 1);
            } else {
                $output = preg_replace("/^[\d]{2}$dateDelimiter/", "m/", $output, 1);
                $output = preg_replace("/[\d]{2}$dateDelimiter/", "d/", $output, 1);
            }
        }
        
        if (preg_match("/\s/", $output)) {
            
            if (preg_match("/\s[\d]{1}$/", $output)) {
               $output = preg_replace("/\s[\d]{1}$/", " H", $output, 1);
            }
            
            $output = preg_replace("/\s[\d]{2}/", " H", $output, 1);
            $output = preg_replace("/[\d]{2}/", "i", $output, 1);
            $output = preg_replace("/[\d]{2}/", "s", $output, 1);
            
            if (preg_match("/i:[\d]{1}/", $output)) {
                $this->userInput = $this->userInput."0";
                $output = preg_replace("/i:[\d]{1}/", "i:s", $output, 1);
            }
            
            if (preg_match("/H:[\d]{1}/", $output)) {
                $this->userInput = $this->userInput."0";
                $output = preg_replace("/H:[\d]{1}/", "H:i", $output, 1);
            }
            
            
        }
        return $output;
    }
}
