package org.xcolab.client.members;

import org.xcolab.client.members.pojo.Member;
import org.xcolab.client.members.pojo.PlatformTeam;
import org.xcolab.util.http.ServiceRequestUtils;
import org.xcolab.util.http.caching.CacheKeys;
import org.xcolab.util.http.caching.CacheName;
import org.xcolab.util.http.client.CoLabService;
import org.xcolab.util.http.client.RestResource;
import org.xcolab.util.http.client.RestResource1;
import org.xcolab.util.http.client.RestService;
import org.xcolab.util.http.exceptions.EntityNotFoundException;

import java.util.List;

public class PlatformTeamsClient {

    private static final RestService platformTeamService =
            new RestService(CoLabService.MEMBER, ServiceRequestUtils.getNamespace());

    private static final RestResource<PlatformTeam, Long> platformTeamResource =
            new RestResource1<>(platformTeamService, "platformteams", PlatformTeam.TYPES);

    private PlatformTeamsClient() {
    }

    public static List<PlatformTeam> listAllPlatformTeams() {
        return platformTeamResource.list().addRange(0, Integer.MAX_VALUE)
                .withCache(CacheName.PLATFORMTEAM).execute();
    }

    public static PlatformTeam getPlatformTeam(long teamId) throws EntityNotFoundException {
        return platformTeamResource.get(teamId)
                .withCache(CacheName.PLATFORMTEAM)
                .executeChecked();
    }

    public static PlatformTeam createPlatformTeam(String name) {
        PlatformTeam team = new PlatformTeam();
        team.setName(name);
        team = platformTeamResource.create(team).execute();
        ServiceRequestUtils.clearCache(CacheName.PLATFORMTEAM);
        return team;
    }

    public static boolean deletePlatformTeam(PlatformTeam team) {
        boolean result = platformTeamResource.delete(team.getId_())
                .execute();
        ServiceRequestUtils.clearCache(CacheName.PLATFORMTEAM);
        return result;
    }

    public static boolean updatePlatformTeam(PlatformTeam team) {
        ServiceRequestUtils.clearCache(CacheName.PLATFORMTEAM);
        return platformTeamResource.update(team, team.getId_())
                .cacheName(CacheName.PLATFORMTEAM)
                .execute();
    }

    public static List<Member> getTeamMembers(PlatformTeam team) {
        return platformTeamResource
                .service(team.getId_(), "members", Member.TYPES.getTypeReference())
                .getList();
    }

    public static List<PlatformTeam> getTeams(Member member) {
        return platformTeamResource.list()
                .queryParam("userId", member.getId_())
                .withCache(CacheName.PLATFORMTEAM)
                .execute();
    }

    public static boolean addMember(PlatformTeam team, Member member) {
        ServiceRequestUtils.clearCache(CacheName.PLATFORMTEAM);
        return platformTeamResource
                .service(team.getId_(), "members/" + member.getId_(), Boolean.class)
                .put();
    }

    public static boolean removeMember(PlatformTeam team, Member member) {
        ServiceRequestUtils.clearCache(CacheName.PLATFORMTEAM);
        return platformTeamResource
                .service(team.getId_(), "members/" + member.getId_(), Boolean.class)
                .delete();
    }


}

@@ -180,55 +180,8 @@
 ls;%0A
-import org.xcolab.util.http.caching.CacheKeys;%0A
 impo
@@ -223,16 +223,16 @@
 heName;%0A
+
 import o
@@ -1083,24 +1083,25 @@
 ame.PLATFORM
+_
 TEAM).execut
@@ -1290,32 +1290,33 @@
 cheName.PLATFORM
+_
 TEAM)%0A          
@@ -1597,32 +1597,33 @@
 cheName.PLATFORM
+_
 TEAM);%0A        r
@@ -1852,32 +1852,33 @@
 cheName.PLATFORM
+_
 TEAM);%0A        r
@@ -2013,32 +2013,33 @@
 cheName.PLATFORM
+_
 TEAM);%0A        r
@@ -2130,32 +2130,33 @@
 cheName.PLATFORM
+_
 TEAM)%0A          
@@ -2604,24 +2604,25 @@
 ame.PLATFORM
+_
 TEAM)%0A      
@@ -2771,32 +2771,33 @@
 cheName.PLATFORM
+_
 TEAM);%0A        r
@@ -3006,32 +3006,32 @@
 ember member) %7B%0A
-
         ServiceR
@@ -3071,16 +3071,17 @@
 PLATFORM
+_
 TEAM);%0A 

update cache name