/*
 * Copyright 2011 Splunk, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"): you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.splunk.sdk;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;

// UNDONE: need to deal with data representation -- other than straight XML.

/**
 * Mid level splunk sdk API; uses lower level Binding sdk API.
 */

public class Client {

    private final String PATH_APPS = "apps/local/";
    private final String PATH_CAPABILITIES = "authorization/capabilities/";
    private final String PATH_CONF = "configs/conf-%s/"; // (config file)
    private final String PATH_CONFS = "properties/";
    private final String PATH_INDEXES = "data/indexes/";
    private final String PATH_INPUTS = "data/inputs/";
    private final String PATH_JOBS = "search/jobs/";
    private final String PATH_LOGGER = "server/logger/";
    private final String PATH_MESSAGES = "messages/";
    private final String PATH_ROLES = "authentication/roles/";
    private final String PATH_STANZA = "configs/conf-%s/%s"; // (config file, stanza)
    private final String PATH_USERS = "authentication/users/";
    private final String PATH_DEP_SERV = "deployment/server/";
    private final String PATH_DEP_CLI = "deployment/client/";
    private final String PATH_DEP_SERV_CL = "deployment/serverclass/";
    private final String PATH_DEP_TENANT = "deployment/tenants/";
    private final String PATH_DSEARCH_PEER = "search/distributed/peers/";
    private final String PATH_DSEARCH_CONF = "search/distributed/config/";

    private final String XNAME_ENTRY = "{http://www.w3.org/2005/Atom}entry";
    private final String XNAME_CONTENT = "{http://www.w3.org/2005/Atom}content";

    public Binding bind = null;

    public Client() {
        // TODO: wkcfix do we need a default constructor?
    }

    /**
     * Connect to a splunkd instance using default login arguments
     *
     * @throws IOException
     */
    public void connect() throws IOException {
        bind = new Binding();
        bind.login();
    }

    /**
     * Connect to a splunkd instance using supplied arguments. NB: all need to be supplied.
     *
     * @param host     hostname (DNS or IPaddress) of splunkd server
     * @param port     port that splunkd listens on
     * @param username username to login as
     * @param password password to login with
     * @param scheme   either http or https
     * @throws IOException
     */
    public void connect(String host, String port, String username, String password, String scheme) throws IOException {
        bind = new Binding();
        bind.login(host, port, username, password, scheme);
    }

    /**
     * Endpoint class: Represents the class all accesses use. Get/post/delete.
     */

    public class Endpoint extends Binding {
        String path = null;

        public Endpoint() {
            // nothing
        }

        public Endpoint(Binding bind, String pth) {
            if (!pth.endsWith("/")) pth += "/";
            path = pth;
        }
    }

    /**
     * Entity class: Represents a single splunkd object.
     */

    public class Entity extends Endpoint {

        Endpoint endp = null;

        public Entity() {
            // nothing
        }

        public Entity(Binding service, String path) {
            endp = new Endpoint(service, path);
        }

        public HttpURLConnection get() throws IOException {
            // TODO: wkcfix, correct way to reach into extended classes?
            return bind.get(endp.path);
        }
    }

    /**
     * Collection class: Represents a collection of splunkd objects.
     */

    public class Collection extends Entity {

        Entity ent = null;

        public Collection() {
            // nothing
        }

        public Collection(Binding service, String pth) {
            ent = new Entity(service, pth);
            // nothing
        }

        public HttpURLConnection get() throws IOException {
            // TODO: wkcfix, correct way to reach into extended classes?
            return bind.get(ent.endp.path);
        }
    }


    // TODO: wkcfix ... methods or classes?
    /**
     * apps class: Represents the application splunkd objects
     */

    public Entity app(String name) {
        return new Entity(bind, PATH_APPS + name);
    }

    public Collection apps() {
        return new Collection(bind, PATH_APPS);
    }

    /**
     * apps class: Represents the configuration splunkd objects
     */

    public class Conf {

    }

    /**
     * apps class: Represents the Index splunkd objects
     */

    public class Index {

    }

    /**
     * apps class: Represents the Input splunkd objects
     */

    public class Input {

    }

    /**
     * apps class: Represents the Inputs splunkd objects
     */

    public class Inputs {

    }

    /**
     * apps class: Represents the JOB splunkd objects
     */

    public class Job {

    }

    /**
     * apps class: Represents the Jobs splunkd objects
     */

    public class Jobs {

    }

    /**
     * apps class: Represents the Message splunkd objects
     */

    public class Message {

    }
}

@@ -3536,22 +3536,26 @@
 %0A     * 
-Entity
+Collection
  class: 
@@ -3571,14 +3571,21 @@
 s a 
-single
+collection of
  spl
@@ -3595,16 +3595,17 @@
 d object
+s
 .%0A     *
@@ -3624,22 +3624,26 @@
 c class 
-Entity
+Collection
  extends
@@ -3701,22 +3701,26 @@
  public 
-Entity
+Collection
 () %7B%0A   
@@ -3765,22 +3765,26 @@
  public 
-Entity
+Collection
 (Binding
@@ -3801,17 +3801,16 @@
 String p
-a
 th) %7B%0A  
@@ -3849,17 +3849,16 @@
 rvice, p
-a
 th);%0A   
@@ -4066,34 +4066,30 @@
  /**%0A     * 
-Collection
+Entity
  class: Repr
@@ -4093,37 +4093,30 @@
 epresents a 
-collection of
+single
  splunkd obj
@@ -4110,33 +4110,32 @@
 e splunkd object
-s
 .%0A     */%0A%0A    p
@@ -4150,55 +4150,60 @@
 ass 
-Collection extends Entity %7B%0A%0A        Entity ent
+Entity extends Collection %7B%0A%0A        Collection coll
  = n
@@ -4219,34 +4219,30 @@
      public 
-Collection
+Entity
 () %7B%0A       
@@ -4279,34 +4279,30 @@
      public 
-Collection
+Entity
 (Binding ser
@@ -4306,33 +4306,47 @@
 service, String 
-p
+base, String pa
 th) %7B%0A          
@@ -4351,62 +4351,54 @@
     
-ent
+coll
  = new 
-Entity(service, pth);%0A            // nothing
+Collection(service, base + path)  ;
 %0A   
@@ -4566,19 +4566,20 @@
 ind.get(
-ent
+coll
 .endp.pa
@@ -4604,53 +4604,8 @@
  %7D%0A%0A
-%0A    // TODO: wkcfix ... methods or classes?%0A
     
@@ -4757,18 +4757,17 @@
 ATH_APPS
- +
+,
  name);%0A

reorder collection/entity relationship