<?php
defined('IN_WEB') or die( 'Include Error!');
/**
 *  多语言推送
 */

class ModelLang {
    public $prefix    = 'cc_';
    public $langConfig;
    public $cliLangConfig;
    public $moTitleContent = [
        'push' =>[
            'title'   =>[
                'gift'     => 'You got a gift!',
                'spins'    => 'Full of spins',
                'money'    => 'Coins gift for you',
                'steal'    => 'Attention! Someone raided you!',
                'attack'   => 'Watch out! Someone attacked you!',
                'feedback' => 'Your feedback has been replied！',
                'recall'   => 'A big gift bag hit you',
                'activityRank'   => 'Give you some coins',
                'update'   => 'NEW UPDATE',
                'turnTable'=> 'Free coins',
                'mingGame' => 'Free games',
                'rank'     => 'The tournament is on！',
                'collectStealAttack'     => 'Spin to win',
                'doubleSpin'     => 'Quick recovery spins',
                'vipDailyReward' => 'Free spins',
                'vipDay'         => 'A lot of prizes',
            ],
            'content'=>[
                'gift'     => '{0} just sent you a gift, check it right away!',
                'spins'    => 'Spins are full now, build your own world and attack your friends now!',
                'money'    => 'We just sent you {0} COINS, wish you happy every day',
                'steal'    => '{0} just raided {1} coins from you! Revenge now!',
                'attack'   => '{0} just attacked your world! Revenge now!',
                'feedback' => 'Your feedback has been replied！',
                'recall'   => 'You have a super gift for you to collect.',
                'activityRank'   => 'You have rewards to receive!',
                'update'   => 'Update Game to unlock awesome new features！',
                'turnTable'=> 'Big Wheel can turn free!',
                'mingGame' => 'Gold miners can play for free',
                'rank'     => 'The champion rewards 1,000 spins, come on!',
                'collectStealAttack'     => 'Come and spin wildly',
                'doubleSpin'     => 'Double the speed of spins recovery',
                'vipDailyReward' => 'Dear VIP, your daily benefits are ready!',
                'vipDay'         => 'The fan page posted a redemption code, receive the redemption code, and come to the game to redeem a huge prize',
            ],
        ],
    ];
    private function getLangPath($prefix,$lang){
        return WWWROOT . 'lang/'.$prefix.'lang.'.$lang.'.php';
    }

    private function setLang($lang){
        if(empty($this->langConfig[$lang])){
            $fileLang = self::getLangPath($this->prefix,$lang);
            if(file_exists($fileLang)) {
                $this->langConfig[$lang] = include_once $fileLang;
            }else {
                $this->langConfig[$lang] = include_once  WWWROOT . 'lang/'.$this->prefix.'lang.en.php';
            }
        }
    }

    /**
     *
     * Created by: Owen
     * Created on: 2020/7/6 12:20
     */
    public function setAllLang(){
        $path = WWWROOT . 'lang/';
        $fileName = scandir($path);
        foreach($fileName as $k=>$v){
            if($v=="." || $v==".." || strpos($v,$this->prefix) === false){
                continue;
            }
            $temp = explode('.',$v);
            self::setLang($temp[1]);
        }
    }

    /**
     * 获取语言
     * @param $lang
     * @param $key
     * @param array $arg
     * @return mixed
     * Created by: Owen
     * Created on: 2019/12/13 18:27
     */
    public function getLang($lang,$key,$arg = []){
        self::setLang($lang);
        $keys = explode('.',$key);
        $value = $this->langConfig[$lang];
        if(empty($value)){
            $value = $this->moTitleContent;
        }
        foreach ($keys as $row){
            $value = $value[$row];
        }
        if(empty($value)){
            $value = $key;
        }
        if($value=='push.title.steal'){
            $value = 'Attention! Someone raided you!';
        }
        if($value=='push.content.steal'){
            $value = '{0} just raided {1} coins from you! Revenge now!';
        }
        if($value=='push.title.attack'){
            $value = 'Watch out! Someone attacked you!';
        }
        if($value=='push.content.attack'){
            $value = '{0} just attacked your world! Revenge now!';
        }

        if(!empty($arg) && is_array($arg)){
            foreach ($arg as $key=>$row){
                $value = str_replace("{{$key}}",$row,$value);
            }
        }
        return $value;
    }

    /**
     * 获取所有语言对应的值
     * @param $key
     * @param array $arg
     * @return array
     * Created by: Owen
     * Created on: 2020/7/6 15:40
     */
    public function getAllLang($key,$arg = []){
        $out = [];
        self::setAllLang();
        foreach ($this->langConfig as $lang=>$data){
            $keys = explode('.',$key);
            foreach ($keys as $row){
                $data = $data[$row];
            }
            if(empty($data)){
                $data = $key;
            }

            if(!empty($arg) && is_array($arg)){
                foreach ($arg as $key=>$row){
                    $data = str_replace("{{$key}}",$row,$data);
                }
            }
            $out[$lang] = $data;
        }
        return $out;
    }

}
