/*
 * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
 */
package kr.co.zumo.app.lifeplus.network;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * NetworkStatus
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 9. 18.]   [최초 작성]
 * @since 2018. 9. 18.
 */
public class NetworkStatus {

  public static final String TYPE_WIFI = "WIFI";
  public static final String TYPE_WIMAX = "WIMAX";
  public static final String TYPE_MOBILE = "MOBILE";

  public static final int DISCONNECTED = -1;

  NetworkInfo networkInfo;

  public NetworkStatus(NetworkInfo networkInfo) {
    this.networkInfo = networkInfo;
  }

  /**
   * wifi, mobile 등 네트워크에 연결 되었는지 확인
   *
   * @return
   */
  public boolean isConnected() {
    boolean isConnected;

    try {
      isConnected = networkInfo.isConnected();
    } catch (Throwable e) {
      isConnected = false;
    }

    return isConnected;
  }

  /**
   * 연결된 네트워크의 종류를 반환한다.
   * 연결되지 않았다면 DISCONNECTED(-1) 반환
   *
   * @return ConnectivityManager.TYPE_WIFI ...
   */
  public int getConnectedType() {
    int type;

    try {
      type = networkInfo.getType();
    } catch (Throwable e) {
      type = DISCONNECTED;
    }

    return type;

  }

  /**
   * 네트워크 커넥트 타입을 문자열로 반환한다.
   *
   * @param connectedType ConnectivityManager.TYPE_WIFI
   * @return "WIFI"
   */
  public String getTypeString(int connectedType) {
    String type = null;
    switch (connectedType) {
      case ConnectivityManager.TYPE_WIFI:
        type = TYPE_WIFI;
        break;
      case ConnectivityManager.TYPE_WIMAX:
        type = TYPE_WIMAX;
        break;
      case ConnectivityManager.TYPE_MOBILE:
        type = TYPE_MOBILE;
        break;
      default:
        break;
    }
    return type;
  }
}
