<?php

    /**
     * File for the team creation action.
     *
     * Implements an action function to be called from the {@see Controller}.
     * 
     * @category Action
     */

    /**
     * Logs the user out and redirects to the login page.
     *
     * Reads the POST parameters looking for the following KEYS:
     *  uid
     *  area_type
     *  area
     *  stage
     *  difficulty
     *  ids
     *  name
     *  description
     * Validates the data and creates the team in the database.
     * 
     * @param resource $db Database connection. 
     * @return int 0 on success, negative values on error.
     * @category Action
     * @global int[] Area types.
     * @global int[] Difficulty levels.
     * @global int[] Scenario areas.
     * @global int[] Dungeon sections.
     * @global int[] Elemental rift dungeons.
     * @global int[] Labyrinth stage types.
     */
    function action($db = null){

        $player = filter_input(INPUT_POST, 'uid');
        $area_type = filter_input(INPUT_POST, 'area_type');
        $area = filter_input(INPUT_POST, 'area');
        $stage = filter_input(INPUT_POST, 'stage');
        $difficulty = filter_input(INPUT_POST, 'difficulty');
        $ids = filter_input(INPUT_POST, 'ids');
        $name = filter_input(INPUT_POST, 'name');
        $description = filter_input(INPUT_POST, 'description');
        $id = preg_split("/[\s,]+/", $ids);
        $id_count = sizeof($id);
        if ($name == null || strlen($name) == 0){
            return -1;
        }
        switch ($area_type){
            case AREA_TYPE::SCENARIO:
                if ($stage < 0 || $stage > 7){
                    return -31;
                }
                if (!($difficulty == 0 || property_exists(DIFFICULTY_ID, $difficulty) || $difficulty == DIFFICULTY::EASY)){
                    return -32;
                }
                if ($id_count < 1 || $id_count > 4){
                    return -33;
                }
                if (!property_exists(SCENARIO_ID, $area)){
                    return -34;
                }
                break;
            case AREA_TYPE::CAIROS_DUNGEON:
                if ($stage < 0 || $stage > 10){
                    return -35;
                }
                $difficulty = null;
                if ($id_count < 1 || $id_count > 5){
                    return -36;
                }
                if (!property_exists(DUNGEON_ID, $area)){
                    return -37;
                }
                break;
            case AREA_TYPE::RIFT_DUNGEON:
                $stage = null;
                $difficulty = null;
                if ($id_count < 1 || $id_count > 6){
                    return -39;
                }
                if (!property_exists(ELEMENTAL_RIFT_DUNGEON_ID, $area)){
                    return -40;
                }
                break;
            case AREA_TYPE::RIFT_RAID:
                if ($stage < 0 || $stage > 5){
                    return -41;
                }
                $difficulty = null;
                if ($id_count < 1 || $id_count > 6){
                    return -42;
                }
                $area = null;
                break;
            case AREA_TYPE::ARENA:
                $area = null;
                $stage = null;
                $difficulty = null;
                break;
            case AREA_TYPE::GUILD_WAR:
                $area = null;
                $stage = null;
                $difficulty = null;
                break;
            case AREA_TYPE::GUILD_SIEGE:
                $area = null;
                $stage = null;
                $difficulty = null;
                break;
            case AREA_TYPE::TARTARUS_LABYRINTH:
                $stage = null;
                if ($difficulty < 0 || !property_exists(DIFFICULTY_ID, $difficulty)){
                    return -43;
                }
                if ($id_count < 1 || $id_count > 5){
                    return -44;
                }
                if (!property_exists(LABYRINTH_STAGES_ID, $area)){
                    return -45;
                }
                break;
            case AREA_TYPE::TRIAL_OF_ASCENSION:
                if ($difficulty != 0 && $difficulty != DIFFICULTY::NORMAL && $difficulty != DIFFICULTY::HARD){
                    return -46;
                }
                $stage = null;
                if ($id_count < 1 || $id_count > 5){
                    return -47;
                }
                $area = null;
                break;
            case AREA_TYPE::DIMENSIONAL_HOLE:
                if ($stage < 0 || $stage > 5){
                    return -48;
                }
                $difficulty = null;
                if ($id_count < 1 || $id_count > 4){
                    return -49;
                }
                if (!property_exists(DUNGEON_ID, $area)){
                    return -50;
                }
                break;
            case AREA_TYPE::DIMENSIONAL_RIFT:
                if ($difficulty != 0 && $difficulty != DIFFICULTY::NORMAL && $difficulty != DIFFICULTY::HARD){
                    return -51;
                }
                $stage = null;
                if ($id_count < 1 || $id_count > 5){
                    return -52;
                }
                $area = null;
                break;
            default:
                return -3;
        }
        $unique_id = array_unique($id);
        if ($id_count != sizeof($unique_id)){
            return -4;
        }
        $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
        $q = $db->query($s);
        $r = $q->fetchArray(SQLITE3_ASSOC);
        $team_id = $r["id"];
        $statement = $db->prepare('INSERT INTO team (uid, id, name, description, area_type, area, stage, difficulty) VALUES (:uid, :id, :name, :description, :area_type, :area, :stage, :difficulty);');
        $statement->bindValue(':uid', $player);
        $statement->bindValue(':id', $team_id);
        $statement->bindValue(':name', $name);
        $statement->bindValue(':description', $description);
        $statement->bindValue(':area_type', $area_type);
        $statement->bindValue(':area', $area);
        $statement->bindValue(':stage', $stage);
        $statement->bindValue(':difficulty', $difficulty);
        $res = $statement->execute();
        if (!$res){
            return -5;
        }
        foreach ($id as $i){
            $s = "INSERT INTO team_unit VALUES ($team_id, $i);";
            $db->query($s);
        }
        return 0;
    }
?>

