<?php
class build_lan extends Controller
{

    private $source_dir;
    private $target_dir;

    public function run($params = null)
    {
        $support_langs = array_keys($this->config->get('support_langs'));
        if(!$this->config->get('config_trans_api_url')){
            die("请配置翻译接口地址API url，联系QQ:279197963");
        }
        $from = $params['from']??'en';
        $to = $params['to']??'ms';
        if(!in_array($from,$support_langs) || !in_array($to,$support_langs)){
            die('Can not recognition language');
        }
        $lan_name = $params['dir']??'en-gb';
        $source_dir = $params['source_dir']??'';
        $target_dir = $params['target_dir']??'';
        //单独翻译某个目录
        if($source_dir && $target_dir){
            if($source_dir == $target_dir){return;}
            $this->source_dir = $source_dir;
            $this->target_dir = $target_dir;
            if(!file_exists($this->source_dir)){echo 'Can not find dirctory!';return;}
            if(!file_exists($this->target_dir)){mkdir($this->target_dir,'0777') or die('create folder access faile');}
            $this->create_dir($this->source_dir);
            $this->tranlate_dir($this->source_dir,$from,$to);
        }else{
        //整个前后台都翻译
        $dir_arr  = [DIR_APPLICATION.'language/',DIR_APPLICATION.'../admin/language/'];
        foreach ($dir_arr as $dir){
        if($lan_name == $to){continue;}
        $this->source_dir = $dir.$lan_name;
        $this->target_dir = $dir.$to;
        if(!file_exists($this->source_dir)){echo 'Can not find dirctory!';continue;}
        if(!file_exists($this->target_dir)){mkdir($this->target_dir,'0777') or die('create folder access faile');}
        $this->create_dir($this->source_dir);
        $this->tranlate_dir($this->source_dir,$from,$to);
        rename( $this->target_dir.'/'.$lan_name.'.php',$this->target_dir.'/'.$to.'.php') ;
        rename( $this->target_dir.'/'.$lan_name.'.png',$this->target_dir.'/'.$to.'.png') ;
        }
        }
    }

    private function create_dir($source){
        $read_dir = opendir($source);
        //创建好目录
        while($filename = readdir($read_dir))
        {
            if($filename != "." && $filename != "..")
            {
                $file = $source."/".$filename;
                $cur = $this->target_dir. str_replace($this->source_dir,'',$file);
                if(is_dir($file))
                {
                    @mkdir($cur,'0777',true);
                    $this->create_dir($file); //使用递归
                }
            }
        }
        closedir($read_dir);
    }

    private function tranlate_dir($dir,$from,$to){
        $read_dir = opendir($dir);
        //创建好目录
        while($filename = readdir($read_dir))
        {
            if($filename != "." && $filename != "..")
            {
                $file = $dir."/".$filename;
                $cur = $this->target_dir. str_replace($this->source_dir,'',$file);
                if(is_dir($file))
                {
                    $this->tranlate_dir($file,$from,$to); //使用递归
                }else
                {
                    //翻译文件$file并复制到新目录
                    $this->tranlate_file($file,$cur,$from,$to);
                }
            }
        }
        closedir($read_dir);
    }

    private function tranlate_file($file,$target_file,$from,$to){
        $logs = new \Log('autotranslate-' . date("Ymd"));
        //不是pup文件直接复制
        $prefix = substr($file, strrpos($file, '.')+1);
        if($prefix != 'php'){ @copy($file,$target_file);return;}
        $header = array(
            'Content-Type: application/json'
        );
        $_ = [];
        $html = '<?php ' . PHP_EOL;
        include_once $file;
        $data = $this->translater->get(array_values($_),$from,$to);
        if($data){
             $keys = array_keys($_);
             if(count($keys) == count($data)){
                 foreach ($keys as $i=>$key){
                     $html .= '$_[\''.$key.'\'] = \''.str_replace("'",'\\\'',$data[$i]).'\';'.PHP_EOL;
                 }
                 print('translate:'.$file.PHP_EOL);
                 file_put_contents($target_file,$html);
             }else{
                 $logs->write('fail file:'.$file);
                 //避免报错
                 @copy($file,$target_file);return;
             }
        }
    }

}