<?php

namespace App\Traits;

use App\Models\Lang;

trait ViewLangTrait
{
    /**
     * @return $array
     */
    public static function language($filename = '', $lang = '') 
    {
        $language['list'] = self::fileArray($filename, $lang);
        $language['map']  = self::fileArray("{$filename}_map");

        return $language;
    }

    /**
     * @return $array
     */
    public static function addLanguage($filename = '', $data) 
    {
        $defaultLang  = config('setting.lang');
        
        foreach ($data['description'][$defaultLang] as $key => $value) {
            $language = self::language($filename, $defaultLang);
            $language['list'] = array_add($language['list'], $data['name'], $value);
            $language['map']  = array_add($language['map'], $data['name'], $data['summary']);
            self::updateLanguage($filename, $language, $defaultLang);
        }

        if (!empty(array_except($data['description'], $defaultLang))) {
            foreach (array_except($data['description'], $defaultLang) as $key => $value) {
                foreach ($value as $k => $val) {
                    $newLanguage = self::fileArray($filename, $key) + [$data['name'] => $val];
                    $newLanguage['list'] = array_merge($language['list'], $newLanguage);
                    self::updateLanguage($filename, $newLanguage, $key);
                }
            }
        }
        
        return true;
    }

    /**
     * @return $array
     */
    public static function removeLanguage($filename = '', $name) 
    {
        $langs        = Lang::lists('name', 'slug')->toArray();
        $defaultLang  = config('setting.lang');
        $language     = self::language($filename, $defaultLang);
        $language['list'] = array_except($language['list'], $name);
        $language['map']  = array_except($language['map'], $name);
        self::updateLanguage($filename, $language, $defaultLang);

        if (!empty(array_except($langs, $defaultLang))) {
            foreach (array_except($langs, $defaultLang) as $key => $value) {
                $language['list'] = array_except(self::fileArray($filename, $key), $name);
                self::updateLanguage($filename, $language, $key);
            }
        }
        
        return true;
    }

    /**
     * @return success
     */
    public static function updateLanguage($filename = '', array $array = [], $lang = '') 
    {
        $theme = config('setting.theme');

        $path = public_path("themes/{$theme}/langs");

        if (isset($array['map'])) {
            \File::put($path.'/'.$filename.'_map.php', self::getContent($array['map']));
        }

        $langPath = $path.'/'.$lang;

        \File::isDirectory($langPath) or \File::makeDirectory($langPath, 0777, true, true);
        
        return \File::put($langPath.'/'.$filename.'.php', self::getContent($array['list']));
    }

    /**
     * @return string
     */
    public static function getContent($array) 
    {
        $content = "<?php\nreturn ".var_export($array, true).';';

        return $content;
    }

    /**
     * @return Array
     */
    public static function fileArray($filename = '', $lang = '') 
    {
        $theme = config('setting.theme');

        $path = public_path("themes/{$theme}/langs/{$lang}/{$filename}.php");

        return file_exists($path) ? require $path : [];
    }

   
}
