<?php
namespace Base\Service;

use Base\Facade\Request;

class GameService {
    const DOWNLOAD_SUPER = 4;
    const DOWNLOAD_BETA = 2;
    const DOWNLOAD_NORMAL = 1;

    public function getDownLoadWaysValue(array $downloadWays)
    {
        $value = 0;
        foreach ($downloadWays as $downloadWay) {
            $value |= $downloadWay;
        }
        return $value;
    }

    public function saveBaseGame($params)
    {
        $baseGame = null;
        $conditions = [];

        $data = [
            'name' => $params['name']
        ];

        $gameIds = [$params['id']];
        if (isset($params['relation_game_id']) && $params['relation_game_id']) {
            $gameIds[] = $params['relation_game_id'];
        }
        $conditions['_logic'] = 'or';
        $conditions['android_game_id'] = ['in', $gameIds];
        $conditions['ios_game_id'] = ['in', $gameIds];
        if ($params['sdk_version'] == 1) {
            $data['android_game_id'] = $params['id'];
        } elseif ($params['sdk_version'] == 2) {
            $data['ios_game_id'] = $params['id'];
        }

        $baseGame = M('base_game', 'tab_')->where($conditions)->find();
        if (!$baseGame) {
            return M('base_game', 'tab_')->add($data);
        } else {
            return M('base_game', 'tab_')->where(['id' => $baseGame['id']])->save($data);
        }
    }

    public function getGames(array $ids = null, $fields = '*')
    {
        $map = [];
        if (is_null($ids)) {
            $map['_string'] = '1=1';
        } elseif (count($ids) == 0) {
            return [];
        } else {
            $map['id'] = ['in', $ids];
        }
        $rules = M('game', 'tab_')->field($fields)->where($map)->select();
        return index_by_column('id', $rules);
    }

    public function getGameTypes(array $ids = null, $fields = '*')
    {
        $map = [];
        if (is_null($ids)) {
            $map['_string'] = '1=1';
        } elseif (count($ids) == 0) {
            return [];
        } else {
            $map['id'] = ['in', $ids];
        }
        $rules = M('game_type', 'tab_')->field($fields)->where($map)->select();
        return index_by_column('id', $rules);
    }

    public function getBaseGames(array $ids = null, $fields = '*')
    {
        $map = [];
        if (is_null($ids)) {
            $map['_string'] = '1=1';
        } elseif (count($ids) == 0) {
            return [];
        } else {
            $map['id'] = ['in', $ids];
        }
        $games = M('base_game', 'tab_')->field($fields)->where($map)->select();
        return index_by_column('id', $games);
    }

    public function getGamesByType($gameTypeId, $fields = '*')
    {
        $map = [];
        if ($gameTypeId == 0) {
            $map['_string'] = '1=1';
        } else {
            $map['game_type_id'] = $gameTypeId;
        }
        $games = M('game', 'tab_')->field($fields)->where($map)->select();
        return index_by_column('id', $games);
    }

    public function getBaseGamesByType($gameTypeId, $fields = '*')
    {
        $map = [];
        if ($gameTypeId == 0) {
            $map['_string'] = '1=1';
        } else {
            $games = $this->getGamesByType($gameTypeId, 'id');
            if (count($games) == 0) {
                return [];
            }
            $gameIds = array_column($games, 'id');
            $map['android_game_id'] = ['in', $gameIds];
            $map['ios_game_id'] = ['in', $gameIds];
            $map['_logic'] = 'or';
        }
        $baseGames = M('base_game', 'tab_')->field($fields)->where($map)->select();
        return index_by_column('id', $baseGames);
    }
    
    public function isUniqueCodeExists($uniqueCode, $exceptId = 0, $exceptType = 'id')
    {
        $map = ['unique_code' => $uniqueCode];
        if ($exceptId > 0) {
            $map[$exceptType] = ['neq', $exceptId];
        }
        $count = M('game', 'tab_')->where($map)->count();
        if ($count > 0) {
            return true;
        }
        return false;
    }
}