<?php
namespace App\Http\Controllers\Admin;

use App\AdminConfig;
use App\Http\Controllers\Controller;
use App\Http\helper\NumConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ConfigController extends Controller
{
    /**
     * @Desc: 配置列表
     * @Author: woann <304550409@qq.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function configList(Request $request)
    {
        $wd   = $request->input('wd');
        $list = AdminConfig::searchCondition($wd)->paginate(10);
        return view('admin.config', ['list' => $list, 'wd' => $wd]);
    }

    /**
     * @Desc: 添加配置
     * @Author: woann <304550409@qq.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function configAddView(Request $request)
    {
        return view('admin.config_add');
    }

    public function configAdd(Request $request)
    {
        $data   = $request->post();
        $config = new AdminConfig();
        $config->fill($data)->save();
        return $this->json(200, '添加成功');
    }

    /**
     * @Desc: 修改配置信息
     * @Author: woann <304550409@qq.com>
     * @param Request $request
     * @param $id
     * @return \Illuminate\View\View
     */
    public function configUpdateView(Request $request, $id)
    {
        return view('admin.config_update', ['config' => AdminConfig::findOrFail($id)]);
    }

    public function configUpdate(Request $request, $id)
    {
        $config = AdminConfig::findOrFail($id);
        $data   = $request->post();
        $config->fill($data);
        $config->save();
        return $this->json(200, '修改成功');
    }

    /**
     * @Desc: 删除配置
     * @Author: woann <304550409@qq.com>
     * @param $id
     * @return mixed
     */
    public function configDel($id)
    {
        AdminConfig::findOrFail($id)->delete();
        return $this->json(200, '删除成功');
    }


    /**
     * 宝藏拉霸配置
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function treasureSlot()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 3018;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            foreach (range(0, 4) as $v1) {
                $key = "jackpot_{$kindID}_{$v}_{$v1}";
                $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
                $descriptions[$key] = __('config_descriptions.jackpot_pool', ['room' => $rooms[$v], 'pool' => $v1]);
                $key = "jackpotBase_{$kindID}_{$v}_{$v1}";
                $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
                $descriptions[$key] = __('config_descriptions.jackpot_base', ['room' => $rooms[$v], 'pool' => $v1]);
                $key = "jackpotRate_{$kindID}_{$v}_{$v1}";
                $config[$key] = $redis->get($key).'%';
                $descriptions[$key] = __('config_descriptions.jackpot_rate', ['room' => $rooms[$v], 'pool' => $v1]);
            }
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => '宝藏',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }

    /**
     * 宝藏拉霸配置
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function newolympusSlot(Request $request)
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 6001;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "jackpot_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.jackpot_pool_single', ['room' => $rooms[$v]]);
            $key = "jackpotBase_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.jackpot_base_single', ['room' => $rooms[$v]]);
            $key = "jackpotRate_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) . '%';
            $descriptions[$key] = __('config_descriptions.jackpot_rate_single', ['room' => $rooms[$v]]);
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) . '%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => '新宙斯',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }

    public function LightSlot()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 3019;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => '闪电',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }


    public function Joker5x1()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 5002;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high'),
            4 => __('config_descriptions.room.super')
        ];
        $config = [];
        foreach (range(1, 4) as $v) {
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => 'MasterJoker 水果单线',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }
    public function FortuneTiger()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 5006;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => 'FortuneTiger 金老虎',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }
    public function FortuneOX()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 5007;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => 'FortuneOX 金牛',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }
    public function FortuneRabbit()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 5007;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => 'FortuneRabbit 金兔',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }
    public function FishingPlus()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 5005;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => 'BigBass钓鱼',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }

    public function gameConfigUpdate($key, Request $request)
    {
        if ($request->isMethod('post')) {
            $Value = (int)$request->Value;

            $redis = Redis::connection('ServerGameRedis');
            if (strpos($key , 'RechargeMaxPercent_') !== false
                || strpos($key, 'jackpotRate_') !== false) {

            } else {
                $Value = $Value * NumConfig::NUM_VALUE;
            }
            if ($Value < 0) {

                $redis->decrBy($key, abs($Value));

            } else {

                $redis->incrBy($key, $Value);
            }

            return apiReturnSuc();
        } else {
            return view('admin.config.update', compact('key'));
        }
    }


    public function Halloween()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 3020;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            foreach (range(0, 3) as $v1) {
                $key = "jackpot_{$kindID}_{$v}_{$v1}";
                $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
                $descriptions[$key] = __('config_descriptions.jackpot_pool', ['room' => $rooms[$v], 'pool' => $v1]);
                $key = "jackpotBase_{$kindID}_{$v}_{$v1}";
                $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
                $descriptions[$key] = __('config_descriptions.jackpot_base', ['room' => $rooms[$v], 'pool' => $v1]);
                $key = "jackpotRate_{$kindID}_{$v}_{$v1}";
                $config[$key] = $redis->get($key).'%';
                $descriptions[$key] = __('config_descriptions.jackpot_rate', ['room' => $rooms[$v], 'pool' => $v1]);
            }
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key).'%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => '万圣节',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }

    public function Fruit9x()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 3014;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.trial'),
            2 => __('config_descriptions.room.primary'),
            3 => __('config_descriptions.room.mid'),
            4 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 4) as $v) {
            $key = "jackpot_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.jackpot_pool_single', ['room' => $rooms[$v]]);
            $key = "jackpotBase_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.jackpot_base_single', ['room' => $rooms[$v]]);
            $key = "jackpotRate_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) . '%';
            $descriptions[$key] = __('config_descriptions.jackpot_rate_single', ['room' => $rooms[$v]]);
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) . '%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => '水果9线',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }

    public function Hive()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 3021;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "jackpot_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.jackpot_pool_single', ['room' => $rooms[$v]]);
            $key = "jackpotBase_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.jackpot_base_single', ['room' => $rooms[$v]]);
            $key = "jackpotRate_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) . '%';
            $descriptions[$key] = __('config_descriptions.jackpot_rate_single', ['room' => $rooms[$v]]);
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) . '%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => '蜜蜂',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }
    public function Aztec()
    {
        $redis = Redis::connection('ServerGameRedis');
        $kindID = 5004;
        $descriptions = [];
        $rooms = [
            1 => __('config_descriptions.room.primary'),
            2 => __('config_descriptions.room.mid'),
            3 => __('config_descriptions.room.high')
        ];
        $config = [];
        foreach (range(1, 3) as $v) {
            $key = "FreeWinMax_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.free_win_max', ['room' => $rooms[$v]]);
            $key = "RechargeControl_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) / NumConfig::NUM_VALUE;
            $descriptions[$key] = __('config_descriptions.recharge_control', ['room' => $rooms[$v]]);
            $key = "RechargeMaxPercent_{$kindID}_{$v}";
            $config[$key] = $redis->get($key) . '%';
            $descriptions[$key] = __('config_descriptions.recharge_max_percent', ['room' => $rooms[$v]]);
        }

        return view('admin.config.treasure_slot', [
            'game_name' => 'Aztec Spell',
            'config' => $config,
            'descriptions' => $descriptions
        ]);
    }

}
