<?php
    function action($db = null){
        global $AREA_TYPE;
        global $DIFFICULTY;
        global $SCENARIO;
        global $DUNGEON;
        global $ELEMENTAL_RIFT_DUNGEON;
        global $LABYRINTH_STAGES;
        global $UID;

        //$player = SQLite3::escapeString($_POST["uid"]);
        $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');
        error_log("STAGE1: " . $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 || in_array($difficulty, $DIFFICULTY) || $difficulty == $DIFFICULTY["EASY"])){
                    return -32;
                }
                if ($id_count < 1 || $id_count > 4){
                    return -33;
                }
                if (!in_array($area, $SCENARIO)){
                    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 (!in_array($area, $DUNGEON)){
                    return -37;
                }
                break;
            case $AREA_TYPE["RIFT_DUNGEON"]:
                $stage = null;
                $difficulty = null;
                if ($id_count < 1 || $id_count > 6){
                    return -39;
                }
                if (!in_array($area, $ELEMENTAL_RIFT_DUNGEON)){
                    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 || !in_array($difficulty, $DIFFICULTY)){
                    return -43;
                }
                if ($id_count < 1 || $id_count > 5){
                    return -44;
                }
                if (!in_array($area, $LABYRINTH_STAGES)){
                    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 (!in_array($area, $DUNGEON)){
                    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;
        }
        
        
        error_log("STAGE2: " . $stage);
        
        $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;
    }
?>
