<?php
class LanguageModel extends CI_Model
{
    private $languageList;
    public function __construct()
    {
        $this->languageList = oo::cfg('lang')['lang_upper'];
    }

    /**
     * 更具语言类型获取语言名
     */
    public function getLangList($type = '') {
        if(!empty($type)){
            return isset($this->languageList[$type]) ? $this->languageList[$type] : '';
        }
        return $this->languageList;
    }

    public function getJson($type = '',$lang = ''){
        $tb = otable::languageConfig();
        if($type && $lang){
            $sql = "SELECT * FROM {$tb} WHERE type = '{$type}' AND lang = '{$lang}' limit 1";
            $ret = oo::commonOprDb('slavedb')->getOne($sql,MYSQLI_ASSOC);
            $ret['content'] = json_decode(base64_decode($ret['content']),true);
            return [$ret['type']=>$ret['content']];
        }else if($type || $lang){
            $sql = "SELECT * FROM {$tb} WHERE (type = '{$type}') or (lang = '{$lang}')";
            $ret = oo::commonOprDb('slavedb')->getAll($sql,MYSQLI_ASSOC);
            $result = [];
            foreach($ret as $k => $v){
                $v['content'] = json_decode(base64_decode($v['content']),true);
                $result[$v['type']][$v['lang']] = $v['content'];
            }
            return $result;
        }else{
            $sql = "SELECT * FROM {$tb}";
            $ret = oo::commonOprDb('slavedb')->getAll($sql,MYSQLI_ASSOC);
            $result = [];
            foreach($ret as $k => $v){
                $v['content'] = json_decode(base64_decode($v['content']),true);
                $result[$v['type']] = $v['content'];
            }
            return $result;
        }
    }

    public function saveJson($type = '',$lang = '',$content = []){
        $tb = otable::languageConfig();
        $sql = "SELECT COUNT(*) AS count FROM {$tb} WHERE type = '{$type}' AND lang = '{$lang}' limit 1";
        $ret = oo::commonOprDb('slavedb')->getOne($sql,MYSQLI_ASSOC);
        $contentTemp = base64_encode(json_encode($content,JSON_UNESCAPED_UNICODE));
        if($ret['count']){
            $sql = "UPDATE {$tb} SET content='{$contentTemp}' WHERE type='{$type}' AND lang = '{$lang}'";
        }else{
            $sql = "INSERT INTO {$tb} (type,lang,content) VALUES ('{$type}','{$lang}','{$contentTemp}')";
        }
        $rs = oo::commonOprDb('games')->query($sql);
        if($rs){
            $result = self::getJson('',$lang);
            $saveResult = [];
            foreach($result as $typeTemp => $row){
                $saveResult[$typeTemp] = $row[$lang];
            }
            $lang2 = $this->lang2($lang);
            $lang3 = $this->lang3($lang);
            $saveResult = array_merge($saveResult,$lang2,$lang3);
            $configName = $lang;
            $filename   = $lang.time().".json";
            oo::commonOprModel("json")->SaveJson($saveResult,$filename) && oo::commonOprModel("json")->setStaticConfig($configName,$filename);
        }
        return $rs;
    }

    public function delJson($type = '',$lang = ''){
        if($type || $lang){
            return false;
        }
        $tb = otable::languageConfig();
        $sql = "DELETE FROM {$tb} WHERE type = '{$type}' AND lang = '{$lang}'";
        $ret = oo::commonOprDb('games')->query($sql);
        return $ret;
    }

    public function lang2($lang){
        $tb = otable::languageConfig(2);
        $list = oo::commonOprDb('slavedb')->getAll("SELECT * FROM {$tb} WHERE `lang`='{$lang}'",MYSQLI_ASSOC);
        $data = [];
        foreach ($list as $row){
            $keyArr = explode('.',$row['key']);
            $this->langRecursion($data,$keyArr,0,$row['content']);
        }
        return $data;
    }

    public function lang3($lang){
        $tb = otable::shareConfig();
        $list = oo::commonOprDb('slavedb')->getAll("SELECT * FROM {$tb}",MYSQLI_ASSOC);
        $data = [];
        foreach ($list as $row){
            $content = json_decode($row['content'],true);
            $data[$row['key']] = $content[$lang];
        }
        return ['SHARE2'=>$data];
    }

    public function langRecursion(&$data,$keyArr,$deep,$content){
        if(count($keyArr)>=1){
            if(count($keyArr) - 1 == $deep){
                $data[$keyArr[$deep]] = $content;
            }else{
                $this->langRecursion($data[$keyArr[$deep]],$keyArr,$deep+1,$content);
            }
        }
    }

    public function setLangJson(){
        $langArr = array_keys($this->languageList);
        foreach ($langArr as $lang){
            $data   = [];
//            $tb = otable::languageConfig();
//            $list = oo::commonOprDb('slavedb')->getAll("SELECT * FROM {$tb} WHERE `lang`='{$lang}'",MYSQLI_ASSOC);
//            foreach ($list as $row){
//                $data[$row['type']] = json_decode(base64_decode($row['content']),true);
//                $lang2 = $this->lang2($lang);
//                $lang3 = $this->lang3($lang);
//                $data = array_merge($data,$lang2,$lang3);
//                $configName = $lang;
//                $filename   = $lang.time().".json";
//                oo::commonOprModel("json")->SaveJson($data,$filename) && oo::commonOprModel("json")->setStaticConfig($configName,$filename);
//            }
            $lang2 = $this->lang2($lang);
            $lang3 = $this->lang3($lang);
            $data = array_merge($data, $lang2,$lang3);
            $configName = $lang;
            $filename   = $lang.time().".json";
            oo::commonOprModel("json")->SaveJson($data,$filename) && oo::commonOprModel("json")->setStaticConfig($configName,$filename);

        }
    }
}
