/*
 * Copyright (C) 2012  Forklabs Daniel Léonard
 *
 * 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 ca.forklabs.wow.net;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import ca.forklabs.baselib.io.StreamUtil;

import java.io.IOException;

/**
 * Class {@code BNetConnectionSpike} demonstrates how to use the connector.
 */
@SuppressWarnings("nls")
public class BNetConnectionSpike extends BNetConnection {

   /**
    * Cheat on the URL.
    */
   @Override
   protected URL createURL(boolean secure, Region region, String resource) throws IOException {
      URL url = new URL("http://everynothing.net/bnetauthtest.php?test=1");
      return url;
      }

   @SuppressWarnings("static-access")
   private static void testEveryNothing() throws Exception {
      System.out.println("=== testEveryNothing() ===");

      BNetConnectionSpike spike_connection = new BNetConnectionSpike();
      HttpURLConnection connection = null;
      try {
         Region region = null;
         String resource = "/bnetauthtest.php?test=1";
         String public_key = "123PUBLIC456";
         String private_key = "987PRIVATE65";

         connection = spike_connection.openConnection(region, resource, public_key, private_key);
         connection.connect();
         int code = connection.getResponseCode();

         InputStream is = connection.getInputStream();
         Charset charset = StreamUtil.UTF_8;
         String text = StreamUtil.readAllText(is, charset);

         System.out.println("HTTP code: " + code);
         System.out.println(text);
         }
      finally {
         if (null != connection) {
            connection.disconnect();
            }
         }
      }

   @SuppressWarnings("static-access")
   private static void testKoreanGuild() throws IOException {
      System.out.println("=== testKoreanGuild() ===");

      BNetConnection spike_connection = new BNetConnection();
      HttpURLConnection connection = null;
      try {
// http://www.wowprogress.com/guild/kr/kargath/즐거운공격대/rating.tier13_25
// World first 25-man Heroic Madness of Deathwing
         Region region = Region.Korea;
         String realm = "카르가스"; // kargath
         String guild = "즐거운공격대";
         String resource = "/api/wow/guild/" + realm + "/" + guild + "?fields=members";

         connection = spike_connection.openConnection(region, resource);
         connection.connect();
         int code = connection.getResponseCode();

         InputStream is = connection.getInputStream();
         Charset charset = StreamUtil.UTF_8;
         String text = StreamUtil.readAllText(is, charset);

         System.out.println("HTTP code: " + code);
         System.out.println(text);
         }
      finally {
         if (null != connection) {
            connection.disconnect();
            }
         }
      }

   /**
    * Entry point.
    * @param   args   ignored.
    * @throws   Exception   if anything goes wrong.
    */
   public static void main(String... args) throws Exception {
      testEveryNothing();
      testKoreanGuild();
      }

   }
