package com.ntels.onecable.admin.common.netatmo;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.util.LogUtil;
import com.ntels.onecable.admin.common.util.Util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * <pre>
 * 1. ClassName :   NetatmoHttpClientImpl
 * 2. FileName  :   NetatmoHttpClientImpl.java
 * 3. Package   :   com.ntels.onecable.admin.common.netatmo
 * 4. Date      :   2016-12-06
 * 5. Author    :   aaron
 * 6. Comment   :   This is just an example of how you can extend NetatmoHttpClient. Tokens are stored in the shared
 * preferences of the app, but you can store them as you wish as long as they are properly returned
 * by the getters. If you want to add your own '/getmeasure' requests, this is also the place to do
 * it.
 * </pre>
 */
public class NetatmoHttpClientImpl extends NetatmoHttpClient {

  private static final String TAG = NetatmoHttpClientImpl.class.getSimpleName();
  private Util util = null;

  Context context;

  SharedPreferences mSharedPreferences;


  /**
   * 생성자.
   *
   * @param context 컨텍스트
   */
  public NetatmoHttpClientImpl(Context context) {
    super(context);
    this.context = context;
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    util = new Util();
  }

  @Override
  public String getClientId() {
    return context.getString(R.string.client_id);
  }

  @Override
  public String getClientSecret() {
    return context.getString(R.string.client_secret);
  }

  @Override
  public String getAppScope() {
    return context.getString(R.string.app_scope);
  }

  @Override
  public void storeTokens(String refreshToken, String accessToken, long expiresAt) {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.putString(NetatmoUtils.KEY_REFRESH_TOKEN, refreshToken);
    editor.putString(NetatmoUtils.KEY_ACCESS_TOKEN, accessToken);
    editor.putLong(NetatmoUtils.KEY_EXPIRES_AT, expiresAt);
    editor.apply();
  }

  @Override
  public void clearTokens() {
    SharedPreferences.Editor editor = mSharedPreferences.edit();
    editor.clear();
    editor.apply();
  }

  @Override
  public String getRefreshToken() {
    return mSharedPreferences.getString(NetatmoUtils.KEY_REFRESH_TOKEN, null);
  }

  @Override
  public String getAccessToken() {
    return mSharedPreferences.getString(NetatmoUtils.KEY_ACCESS_TOKEN, null);
  }

  @Override
  public long getExpiresAt() {
    return mSharedPreferences.getLong(NetatmoUtils.KEY_EXPIRES_AT, 0);
  }

  public ArrayList<HashMap<String, Object>> parseNetatmoCamera(JSONObject jsonObject) {
    ArrayList<HashMap<String, Object>> alCameraList = new ArrayList<>();
    if (!jsonObject.isNull("body")) {
      try {
        if (!jsonObject.getJSONObject("body").isNull("homes")) {
          JSONArray jaHomes = jsonObject.getJSONObject("body").getJSONArray("homes");
          for (int i = 0; i < jaHomes.length(); i++) {
            JSONArray jaCameras = ((JSONObject) jaHomes.get(i)).getJSONArray("cameras");
            if (jaCameras != null) {
              for (int j = 0; j < jaCameras.length(); j++) {
                HashMap<String, Object> camera = util.jsonToMap(jaCameras.get(j));
                alCameraList.add(camera);
                LogUtil.i(TAG, "camera " + (alCameraList.size() - 1) + " : " + camera.toString());
              }
            }
          }
        }
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    return alCameraList;
  }
}
