<?php
/**
 * Game.php UTF-8
 * 游戏处理
 *
 * @date    : 2017/11/16 17:31
 *
 * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 * @author  : wuyonghong <wyh@huosdk.com>
 * @version : HUOSDK 8.0
 */

namespace huo\controller\game;

use huo\controller\common\Base;
use huo\controller\gift\Gift;
use huo\controller\request\Channel;
use huo\controller\request\Game as GameRq;
use huo\logic\game\GameLogic;
use huo\logic\posts\PostsLogic;
use huo\model\game\GameversionModel;
use huolib\constant\GameConst;
use huolib\status\GameStatus;
use huomp\controller\game\GameMini;

class Game extends Base {
    protected $game_logic;

    public function __construct(GameLogic $game_logic = null) {
        if (null === $game_logic) {
            $this->game_logic = new GameLogic();
        } else {
            $this->game_logic = $game_logic;
        }
    }

    /**
     * 获取游戏 app_key
     *
     * @param int $app_id
     *
     * @return bool
     */
    public function getAppkey($app_id = 0) {
        $_game_data = GameCache::ins()->getInfoByAppId($app_id);
        if (empty($_game_data)) {
            return false;
        }
        $_app_key = get_val($_game_data, 'app_key', '');

        return $_app_key;
    }

    /**
     * 获取游戏 切换状态
     *
     * @param int $app_id
     *
     * @return bool
     */
    public function getPaySwitchStatus($app_id = 0) {
        $_game_data = GameCache::ins()->getInfoByAppId($app_id);
        if (empty($_game_data)) {
            return false;
        }
        $_pay_switch = get_val($_game_data, 'pay_switch', 2);

        return $_pay_switch;
    }

    /**
     * 获取游戏 切换状态
     *
     * @param int $app_id
     *
     * @return bool
     */
    public function getPayShowStatus($app_id = 0) {
        $_game_data = GameCache::ins()->getInfoByAppId($app_id);
        if (empty($_game_data)) {
            return false;
        }
        $_pay_switch = get_val($_game_data, 'pay_show', 2);

        return $_pay_switch;
    }

    /**
     * 获取游戏回调地址
     *
     * @param int $app_id
     *
     * @return bool
     */
    public function getCpPaybackUrl($app_id = 0) {
        $_game_data = GameCache::ins()->getInfoByAppId($app_id);
        if (empty($_game_data)) {
            return false;
        }
        $_cp_payback_url = get_val($_game_data, 'cp_payback_url', '');

        return $_cp_payback_url;
    }

    /**
     * 获取游戏地址
     *
     * @param int $app_id
     *
     * @return int|string
     */
    public function getPlayUrl($app_id = 0) {
        if (empty($app_id)) {
            return GameStatus::GAME_ID_EMPTY;
        }
        $_game_info = GameCache::ins()->getInfoByAppId($app_id);
        if (!isset($_game_info['gv'])) {
            return GameStatus::GAME_NOT_EXISTS;
        }
        $_play_url = !empty($_game_info['gv']['0']['package_url']) ? $_game_info['gv']['0']['package_url'] : '';
        if (empty($_play_url)) {
            return GameStatus::GAME_URL_EMPTY;
        }

        return $_play_url;
    }

    /**
     * 获取游戏详情
     *
     * @param int $app_id
     * @param int $mem_id
     *
     * @param int $from 来源
     *
     * @return array
     */
    public function getDetail($app_id, $mem_id = 0, $from = GameConst::GAME_H5) {
        if (empty($app_id)) {
            $this->huoError(GameStatus::INVALID_PARAMS);
        }
        $_map['app_id'] = $app_id;
        $_data = $this->game_logic->getDetail($_map, $field = '');
        if (is_numeric($_data)) {
            $_code = $_data;

            return $this->huoError($_code, GameStatus::getMsg($_code));
        }
        $_list[$_data['game_id']] = $_data;
        unset($_data);
        $_data = (new GameList())->getGameExtInfo($_list);
        $_game_data = $_data[$app_id];
        /* 获取游戏礼包 */
        $_map['app_id'] = $_game_data['game_id'];
        $_gift_list = (new Gift())->getGiftListDetail($mem_id, $_map, '', true);
        if (!empty($_gift_list)) {
            $_game_data['gift'] = $_gift_list['list'];
        }
        if (GameConst::GAME_H5 != $from && GameConst::GAME_MP_BOX != $from) {
            /* 获取最新资讯 */
            $_game_data['news'] = (new PostsLogic())->getTitleByAppId($app_id);
            /* 获取开服信息 */
            $_game_data['serlist'] = (new GameLogic())->getGameServerList($app_id, GameConst::GAME_SERVER_TODAY);
        } elseif (GameConst::GAME_MP_BOX == $from) {
            /* 获取最新资讯 */
            $_game_data['mini_app_id'] = (new GameMini())->getMiniIdByAppId($app_id);
        }
        $_code = GameStatus::NO_ERROR;

        return $this->huoSuccess($_code, GameStatus::getMsg($_code), $_game_data);
    }

    /**
     * 游戏下载接口与下载次数
     *
     * @param $app_id
     *
     * @return array
     */
    public function getDownUrl($app_id, $agent_id = 0) {
        $_down_cnt = $this->game_logic->getDownCnt($app_id);
        $_down_url = $this->game_logic->getDownUrl($app_id);
        $_data['down_cnt'] = $_down_cnt;
        $_data['down_url'] = $_down_url;
        $_code = GameStatus::NO_ERROR;

        return $this->huoSuccess($_code, GameStatus::getMsg($_code), $_data);
    }

    /**
     * 获取更新信息
     *
     * @param GameRq  $game_rq
     * @param Channel $channel
     *
     * @return mixed
     */
    public function getUpInfo(GameRq $game_rq, Channel $channel) {
        // TODO: wuyonghong 2018/1/19 获取更新
        $_rs['up_status'] = 0;
        $_rs['url'] = '';
        $_rs['content'] = '';

        return $_rs;
    }

    /**
     * 获取H5游戏Cp游戏地址
     *
     * @param int $app_id
     *
     * @return string
     */
    public function getH5GameCpUrl($app_id) {
        return (new GameversionModel())->getPackageUrlByAppId($app_id);
    }
}