package com.qiniu.pili;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

import java.util.Date;

public class Meeting {
    private String baseUrl;
    private Gson gson;
    private RPC cli;

    private Meeting(){}

    Meeting(RPC cli ) {
        this.baseUrl = Config.APIHTTPScheme + Config.RTCAPIHost + "/v1";
        this.cli = cli;
        this.gson = new Gson();
    }

    public String createRoom(String ownerId ,String roomName) throws PiliException{
        CreateArgs args = new CreateArgs(ownerId, roomName);
        return createRoom(args);
    }

    public String createRoom(String ownerId) throws PiliException{
        CreateArgs args = new CreateArgs(ownerId);
        return createRoom(args);
    }

    private String createRoom(CreateArgs args) throws PiliException{
        String path = this.baseUrl + "/rooms";
        String json = gson.toJson(args);

        try {
            String resp = cli.callWithJson(path, json);
            RoomName ret = gson.fromJson(resp, RoomName.class);
            return ret.roomName;
        }catch (PiliException e){
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new PiliException(e);
        }
    }

    public Room getRoom(String roomName) throws PiliException{
        String path = this.baseUrl+ "/rooms/" + roomName;
        try {
            String resp = cli.callWithGet(path);
            Room room = gson.fromJson(resp, Room.class);
            return room;
        }catch (PiliException e){
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new PiliException(e);
        }
    }

    public void deleteRoom(String room) throws PiliException{
        String path = this.baseUrl + "/rooms/"+room;
        try {
            cli.callWithDelete(path);
        }catch (PiliException e){
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new PiliException(e);
        }
    }

    public String roomToken(String roomName, String userId, String perm, Date expireAt) throws Exception {
        RoomAccess access = new RoomAccess(roomName, userId, perm, expireAt);
        String json = gson.toJson(access);
        return this.cli.getMac().signRoomToken(json);
    }

    private class RoomAccess{
        @SerializedName("room_name")
        String roomName;
        @SerializedName("user_id")
        String userId;
        @SerializedName("perm")
        String perm;
        @SerializedName("expire_at")
        long expireAt;

        RoomAccess(String roomName, String userId, String perm, Date expireAt){
            this.roomName = roomName;
            this.userId = userId;
            this.perm = perm;
            this.expireAt = expireAt.getTime() / 1000;// seconds
        }
    }

    /**
     *
     */
    public enum Status {
        @SerializedName("0")
        NEW,
        @SerializedName("1")
        MEETING,
        @SerializedName("2")
        FINISHED,
    }

    public class Room {
        @SerializedName("room_name") public String name;
        @SerializedName("room_status") public Status status;
        @SerializedName("owner_id") public String ownerId;
    }

    /**
     *
     */
    private class RoomName {
        @SerializedName("room_name") String roomName;
    }

    private class CreateArgs {
        @SerializedName("owner_id") String ownerId;
        @SerializedName("room_name") String room;

        public CreateArgs(String ownerId, String room) {
            this.ownerId= ownerId;
            this.room = room;
        }
        public CreateArgs(String ownerId){
            this.ownerId = ownerId;
        }
    }

}

@@ -396,32 +396,239 @@
  Gson();%0A    %7D%0A%0A
+    public String createRoom(String ownerId ,String roomName, int userMax) throws PiliException%7B%0A        CreateArgs args = new CreateArgs(ownerId, roomName, userMax);%0A        return createRoom(args);%0A    %7D%0A%0A
     public Strin
@@ -3444,24 +3444,81 @@
 ng ownerId;%0A
+        @SerializedName(%22user_max%22) public int userMaxe;%0A
     %7D%0A%0A    /
@@ -3757,16 +3757,247 @@
 ng room;
+%0A        @SerializedName(%22user_max%22) int userMax;%0A%0A        public CreateArgs(String ownerId, String room, int userMax)%7B%0A            this.ownerId = ownerId;%0A            this.room = room;%0A            this.userMax = userMax;%0A        %7D
 %0A%0A      

add userMax