package com.ntels.onecable.admin.common;

import com.google.firebase.analytics.FirebaseAnalytics;

import com.ntels.onecable.admin.R;
import com.ntels.onecable.admin.common.util.Dlog;
import com.ntels.onecable.admin.common.util.LogUtil;
import com.ntels.onecable.admin.common.util.Util;
import com.ntels.onecable.admin.common.websocket.WebSocketService;
import com.tsengvn.typekit.Typekit;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Base64;
import android.util.Log;
import android.webkit.CookieSyncManager;

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

/**
 * <pre>
 * -----------------------------------------------------------------------------
 * 설명 :  Application 클래스
 * 작성자 : 최진오
 * 작성일자 : 2014-04-06
 * 수정일자 : 2014-04-06 최진오 최초생성
 * ------------------------------------------------------------------------------
 * </pre>
 */
public class AppConfig extends MultiDexApplication {

    private static final String TAG = AppConfig.class.getSimpleName();
    public static boolean DEBUG = false;
    private static volatile AppConfig instance = null;

    private WebSocketService mWebSocketService;
    private ArrayList<HashMap<String, Object>> mCtrtgrpList = new ArrayList<HashMap<String, Object>>();
    private ArrayList<HashMap<String, Object>> mDevicesList = new ArrayList<HashMap<String, Object>>();
    private ArrayList<HashMap<String, Object>> mInvitationList = new ArrayList<HashMap<String, Object>>();
    private Boolean mIsLogin = false;
    private Boolean mIsLogout = false;
    private Boolean mIsExit = false;
    private Boolean mIsManualNotView = false;
    private Boolean mIsPush = false;
    private Boolean mIsAutoLogin = false;
    private Boolean mIsGoErr = false;
    private Boolean mIsIntro = false;
    private Boolean mIsMain = false;
    private Boolean mIsWebSocketAuth = false;
    private Boolean mIsWebSocketConn = false;
    private Boolean mIsCustomerConn = false;
    private String mCustomerCtrtId = "";
    private String mCustomerCustId = "";
    private String mCustomerMemberId = "";
    private String mCustomerPhone = "";
    private String mVer = "";
    private String mUuid = "";
    private String mLoginId = "";
    private String mLoginTermId = "";
    private String mPushToken = "";
    /**
     * 나의 사진 이미지 URL
     */
    private String mMyImageUrl = "";
    /**
     * 닉네임
     */
    private String mNickname = "";
    /**
     * 시간대 ID
     */
    private String mTimezoneId = "";
    /**
     * 로그인 비밀번호
     */
    private String mLoginPass = "";
    private float mScale = 0f;
    private int mDisplayHeight = 0;
    private int mDisplayWidth = 0;
    /**
     * 인증토큰
     */
    private String mAuthToken = "";
    /**
     * url
     */
    private String mUrl = "";
    private String mSmartUrl = "";
    private String mServicehubWebsocketUrl = "";
    private String mServicehubRestUrl = "";
    private String mCloudGatewayUrl = "";
    /**
     * VSaaS
     */
    private String mSipUrl = "";
    private String mTurnUrl = "";
    private int mTurnPort = 0;
    private String mVsaasDomain = "";
    private String mSipPassword = "";
    /**
     * 서비스 아이디
     */
    private String mServiceId = "";
    private String mProjectId = "";
    private String mAppStartId = "";
    /**
     * 언어 ID
     */
    private String sLanguage_Id = "";
    /**
     * 국가 ID
     */
    private String sCountry_Id = "";
    /**
     * 로그인 멤버 아이디
     */
    private String mLoginMemberId = "";
    private String sTemp_Password_Yn = "";
    private String sPush_Type_Mode_Chg_Yn = "";
    private String sPush_Type_Prsnc_Chg_Yn = "";
    /**
     * 초대 여부
     */
    private String sInvitations_Count = "";
    /**
     * (소속된) 홈그룹 명
     */
    private String mCtrtGroupName = "";
    /**
     * (소속된 홈그룹내) 등급 코드 (MAS: 마스터, SIM:심플유저)
     */
    private String mCtrtGroupGradeCode = "";
    /**
     * (소속된 홈그룹내) 홈허브 선택된 디바이스 ID
     */
    private String mSelectHomehubDeviceId = "";
//    /**
//     * (소속된 홈그룹내) 홈허브 접속 상태 (ON / OFF)
//     */
//    private String sSelect_Homegrp_Homehub_Online_State = "";
//    /**
//     * (소속된 홈그룹내) 홈허브 인터넷(MQTT) 접속 상태 (connect / disconnect)
//     */
//    private String sSelect_Homegrp_Homehub_Conn_State = "";
//    /**
//     * (소속된 홈그룹내) 홈허브 동글 접속 상태 (ON / OFF)
//     */
//    private String sSelect_Homegrp_Homehub_Dongle_State = "";
    /**
     * (소속된 홈그룹내) 홈허브 동글 마지막 접속 시간 (포맷 : 년월일시분초)
     */
    private String sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime = "";
    /**
     * (소속된 홈그룹내) 홈허브 MQTT 마지막 접속 시간 (포맷 : 년월일시분초)
     */
    private String sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime = "";
    /**
     * 홈 그룹 디바이스 체크
     */
    private String sSelect_CtrtGroup_Device_Yn = "";
    /**
     * 홈 그룹 명
     */
    private String sSelect_CtrtGroup_Icon = "";
    /**
     * 홈 그룹 주소
     */
    private String sSelect_Location_Addr = "";
    /**
     * 홈 그룹 좌표(포맷 : 위도,경도)
     */
    private String sSelect_Location_Coordinate = "";
    /**
     * 홈 그룹 좌표 반경 (ex : 100, 200, 300)
     */
    private String sSelect_Location_Radius = "";
    /**
     * 서비스허브 어플리케이션 ID
     */
    private String selectServicehubAppId = "";
    /**
     * 서비스허브 리퀘스트 ID
     */
    private int selectServicehubRequestId = 0;
    /**
     * (소속된 홈그룹내) 홈허브 디바이스 Key
     */
    private String mSelectHomehubDeviceKey = "";
    //    private HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
    private Util mUtil = new Util();
    private Boolean mSaveId = false;
    /**
     * (소속된 홈그룹내) 홈허브 디바이스 여부
     */
    private Boolean mIsCtrtGroupHomehub = false;
    /**
     * 고객 ID
     */
    private String mCustId = "";
    /**
     * 고객 퀴즈
     */
    private String mQuiz = "";
    /**
     * 고객 휴대폰 번호
     */
    private String mPhone = "";
    /**
     * 계약 그룹 ID
     */
    private String mCtrtGroupId = "";

    /**
     * 마스터 권한 양도 요청이 오면 Y 아니면 N
     */
    private String mMasterTransferPopupYn = "";

    public static AppConfig getGlobalApplicationContext() {

        if (instance == null)
            throw new IllegalStateException("this application does not inherit com.kakao.GlobalApplication");
        return instance;
    }

    public static AppConfig getInstance() {

        return instance;
    }

    public static String getLanguageType() {
        Locale locale = getGlobalApplicationContext().getResources().getConfiguration().locale;
        Log.i("getLanguageType()", locale.getLanguage());
        return locale.getLanguage();
    }

    @Override
    protected void attachBaseContext(Context base) {

        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /*public synchronized Tracker getTracker(TrackerName trackerId) {

        if (!mTrackers.containsKey(trackerId)) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            Tracker t = (trackerId == TrackerName.APP_TRACKER)
                    ? analytics.newTracker(getString(R.string.google_analytics_property_id))
                    : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(getString(R.string.google_analytics_property_id))
                    : analytics.newTracker(getString(
                    R.string.google_analytics_property_id));
            mTrackers.put(trackerId, t);
        }
        return mTrackers.get(trackerId);
    }*/

    public synchronized FirebaseAnalytics getTracker() {
        return FirebaseAnalytics.getInstance(this);
    }

    /**
     * <PRE> 1. MethodName: onCreate 2. ClassName : 3. Comment   : onCreate 4. Developer : 최진오 5.
     * Created   : 2015. 10. </PRE>
     */
    @Override
    public void onCreate() {

        super.onCreate();
        this.DEBUG = isDebuggable(this);
        instance = this;
        Typekit.getInstance().addNormal(Typekit.createFromAsset(this, "fonts/NotoSans-Light.otf"))
                .addBold(Typekit.createFromAsset(this, "fonts/NotoSans-Regular.otf"));

        patchEOFException();
        CookieSyncManager.createInstance(this);

    }

    public boolean isDebuggable(Context context) {

        boolean debuggable = false;

        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appinfo = pm.getApplicationInfo(context.getPackageName(), 0);
            debuggable = (0 != (appinfo.flags & ApplicationInfo.FLAG_DEBUGGABLE));
        } catch (PackageManager.NameNotFoundException e) {
            /* debuggable variable will remain false */
        }

        return debuggable;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onLowMemory() {

        super.onLowMemory();
    }

    @Override
    public void onTerminate() {

        super.onTerminate();
        instance = null;
    }

    private void patchEOFException() {

        System.setProperty("http.keepAlive", "false");
    }

    public void Labinit() {

        mCtrtgrpList = new ArrayList<HashMap<String, Object>>();
        mDevicesList = new ArrayList<HashMap<String, Object>>();
        mInvitationList = new ArrayList<HashMap<String, Object>>();
        mIsLogin = false;
        mIsLogout = false;
        mIsExit = false;
        mIsManualNotView = false;
        mIsAutoLogin = false;
        mSaveId = false;
        mIsGoErr = false;
        mIsIntro = false;
        mIsMain = false;
        mIsWebSocketAuth = false;
        mIsWebSocketConn = false;
        mIsCustomerConn = false;
        mCustomerCtrtId = "";
        mCustomerCustId = "";
        mCustomerCustId = "";
        mVer = "";
        mUuid = "";
        mLoginId = "";
        mLoginTermId = "";
        mMyImageUrl = "";
        mNickname = "";
        mTimezoneId = "";
        mLoginPass = "";
        mScale = 0f;
        mDisplayHeight = 0;
        mDisplayWidth = 0;
        mPushToken = "";
        mAuthToken = "";
        mUrl = "";
        mSmartUrl = "";
        mServicehubWebsocketUrl = "";
        mServicehubRestUrl = "";
        mCloudGatewayUrl = "";
        mSipUrl = "";
        mTurnUrl = "";
        mTurnPort = 0;
        mVsaasDomain = "";
        mSipPassword = "";
        mServiceId = "";
        mProjectId = "";
        mAppStartId = "";
        sLanguage_Id = "";
        sCountry_Id = "";
        mLoginMemberId = "";
        sSelect_CtrtGroup_Device_Yn = "";
        mCtrtGroupName = "";
        sSelect_CtrtGroup_Icon = "";
        sSelect_Location_Addr = "";
        sSelect_Location_Coordinate = "";
        sSelect_Location_Radius = "";
        mCtrtGroupGradeCode = "";
        sTemp_Password_Yn = "";
        sPush_Type_Mode_Chg_Yn = "";
        sPush_Type_Prsnc_Chg_Yn = "";
        sInvitations_Count = "";
        mSelectHomehubDeviceId = "";
//        sSelect_Homegrp_Homehub_Online_State = "";
//        sSelect_Homegrp_Homehub_Conn_State = "";
//        sSelect_Homegrp_Homehub_Dongle_State = "";
//        sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime = "";
//        sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime = "";
        selectServicehubAppId = "";
        selectServicehubRequestId = 0;
        mSelectHomehubDeviceKey = "";
        mIsCtrtGroupHomehub = false;
        mCustId = "";
        mQuiz = "";
        mPhone = "";
        mCtrtGroupId = "";
    }

    public Bundle saveAppication(Bundle outState) {

        outState.putSerializable("mCtrtgrpList", mCtrtgrpList);
        outState.putSerializable("mDevicesList", mDevicesList);
        outState.putSerializable("mInvitationList", mInvitationList);
        outState.putBoolean("mIsLogin", mIsLogin);
        outState.putBoolean("mIsLogout", mIsLogout);
        outState.putBoolean("mIsExit", mIsExit);
        outState.putBoolean("mIsManualNotView", mIsManualNotView);
        outState.putBoolean("mIsPush", mIsPush);
        outState.putBoolean("mIsAutoLogin", mIsAutoLogin);
        outState.putBoolean("mSaveId", mSaveId);
        outState.putBoolean("mIsGoErr", mIsGoErr);
        outState.putBoolean("mIsIntro", mIsIntro);
        outState.putBoolean("mIsMain", mIsMain);
        outState.putBoolean("mIsWebSocketAuth", mIsWebSocketAuth);
        outState.putBoolean("mIsWebSocketConn", mIsWebSocketConn);
        outState.putBoolean("mIsCustomerConn", mIsCustomerConn);
        outState.putString("mCustomerCtrtId", mCustomerCtrtId);
        outState.putString("mCustomerCustId", mCustomerCustId);
        outState.putString("mCustomerMemberId", mCustomerMemberId);
        outState.putString("mCustomerPhone", mCustomerPhone);
        outState.putString("mVer", mVer);
        outState.putString("mUuid", mUuid);
        outState.putString("mLoginId", mLoginId);
        outState.putString("mLoginTermId", mLoginTermId);
        outState.putString("mMyImageUrl", mMyImageUrl);
        outState.putString("mNickname", mNickname);
        outState.putString("mTimezoneId", mTimezoneId);
        outState.putString("mLoginPass", mLoginPass);
        outState.putFloat("mScale", mScale);
        outState.putInt("mDisplayHeight", mDisplayHeight);
        outState.putInt("mDisplayWidth", mDisplayWidth);
        outState.putString("mPushToken", mPushToken);
        outState.putString("mAuthToken", mAuthToken);
        outState.putString("mUrl", mUrl);
        outState.putString("mSmartUrl", mSmartUrl);
        outState.putString("mServicehubWebsocketUrl", mServicehubWebsocketUrl);
        outState.putString("mServicehubRestUrl", mServicehubRestUrl);
        outState.putString("mCloudGatewayUrl", mCloudGatewayUrl);
        outState.putString("mSipUrl", mSipUrl);
        outState.putString("mTurnUrl", mTurnUrl);
        outState.putInt("mTurnPort", mTurnPort);
        outState.putString("mVsaasDomain", mVsaasDomain);
        outState.putString("mSipPassword", mSipPassword);
        outState.putString("mServiceId", mServiceId);
        outState.putString("mProjectId", mProjectId);
        outState.putString("mAppStartId", mAppStartId);
        outState.putString("sLanguage_Id", sLanguage_Id);
        outState.putString("sCountry_Id", sCountry_Id);
        outState.putString("mLoginMemberId", mLoginMemberId);
        outState.putString("sSelect_CtrtGroup_Device_Yn", sSelect_CtrtGroup_Device_Yn);
        outState.putString("mCtrtGroupName", mCtrtGroupName);
        outState.putString("sSelect_CtrtGroup_Icon", sSelect_CtrtGroup_Icon);
        outState.putString("sSelect_Location_Addr", sSelect_Location_Addr);
        outState.putString("sSelect_Location_Coordinate", sSelect_Location_Coordinate);
        outState.putString("sSelect_Location_Radius", sSelect_Location_Radius);
        outState.putString("mCtrtGroupGradeCode", mCtrtGroupGradeCode);
        outState.putString("sTemp_Password_Yn", sTemp_Password_Yn);
        outState.putString("sPush_Type_Mode_Chg_Yn", sPush_Type_Mode_Chg_Yn);
        outState.putString("sPush_Type_Prsnc_Chg_Yn", sPush_Type_Prsnc_Chg_Yn);
        outState.putString("sInvitations_Count", sInvitations_Count);
        outState.putString("mSelectHomehubDeviceId", mSelectHomehubDeviceId);
//        outState.putString("sSelect_Homegrp_Homehub_Online_State", sSelect_Homegrp_Homehub_Online_State);
//        outState.putString("sSelect_Homegrp_Homehub_Conn_State", sSelect_Homegrp_Homehub_Conn_State);
//        outState.putString("sSelect_Homegrp_Homehub_Dongle_State", sSelect_Homegrp_Homehub_Dongle_State);
//        outState.putString("sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime", sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime);
//        outState.putString("sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime", sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime);
        outState.putString("selectServicehubAppId", selectServicehubAppId);
        outState.putInt("selectServicehubRequestId", selectServicehubRequestId);
        outState.putString("mSelectHomehubDeviceKey", mSelectHomehubDeviceKey);
        outState.putBoolean("mIsCtrtGroupHomehub", mIsCtrtGroupHomehub);
        outState.putString("mCustId", mCustId);
        outState.putString("mQuiz", mQuiz);
        outState.putString("mPhone", mPhone);
        outState.putString("mCtrtGroupId", mCtrtGroupId);

        return outState;
    }

    public void loadAppication(Bundle outState) {

        mCtrtgrpList = (ArrayList<HashMap<String, Object>>) outState.getSerializable("mCtrtgrpList");
        mDevicesList = (ArrayList<HashMap<String, Object>>) outState.getSerializable("mDevicesList");
        mInvitationList = (ArrayList<HashMap<String, Object>>) outState.getSerializable("mInvitationList");
        mIsAutoLogin = outState.getBoolean("mIsAutoLogin");
        mIsExit = outState.getBoolean("mIsExit");
        mIsGoErr = outState.getBoolean("mIsGoErr");
        mIsIntro = outState.getBoolean("mIsIntro");
        mIsMain = outState.getBoolean("mIsMain");
        mIsWebSocketAuth = outState.getBoolean("mIsWebSocketAuth");
        mIsWebSocketConn = outState.getBoolean("mIsWebSocketConn");
        mIsCustomerConn = outState.getBoolean("mIsCustomerConn");
        mIsLogin = outState.getBoolean("mIsLogin");
        mIsLogout = outState.getBoolean("mIsLogout");
        mIsManualNotView = outState.getBoolean("mIsManualNotView");
        mIsPush = outState.getBoolean("mIsPush");
        mSaveId = outState.getBoolean("mSaveId");
        mScale = outState.getFloat("mScale");
        mDisplayHeight = outState.getInt("mDisplayHeight");
        mDisplayWidth = outState.getInt("mDisplayWidth");
        mAuthToken = outState.getString("mAuthToken");
        sCountry_Id = outState.getString("sCountry_Id");
        sLanguage_Id = outState.getString("sLanguage_Id");
        mLoginId = outState.getString("mLoginId");
        mLoginMemberId = outState.getString("mLoginMemberId");
        mLoginPass = outState.getString("mLoginPass");
        mLoginTermId = outState.getString("mLoginTermId");
        mMyImageUrl = outState.getString("mMyImageUrl");
        mNickname = outState.getString("mNickname");
        mPushToken = outState.getString("mPushToken");
        sSelect_CtrtGroup_Device_Yn = outState.getString("sSelect_CtrtGroup_Device_Yn");
        mCtrtGroupGradeCode = outState.getString("mCtrtGroupGradeCode");
        sSelect_CtrtGroup_Icon = outState.getString("sSelect_CtrtGroup_Icon");
        mCtrtGroupName = outState.getString("mCtrtGroupName");
        sSelect_Location_Addr = outState.getString("sSelect_Location_Addr");
        sSelect_Location_Coordinate = outState.getString("sSelect_Location_Coordinate");
        sSelect_Location_Radius = outState.getString("sSelect_Location_Radius");
        mServiceId = outState.getString("mServiceId");
        mProjectId = outState.getString("mProjectId");
        mAppStartId = outState.getString("mAppStartId");
        sTemp_Password_Yn = outState.getString("sTemp_Password_Yn");
        mTimezoneId = outState.getString("mTimezoneId");
        mUrl = outState.getString("mUrl");
        mSmartUrl = outState.getString("mSmartUrl");
        mServicehubWebsocketUrl = outState.getString("mServicehubWebsocketUrl");
        mServicehubRestUrl = outState.getString("mServicehubRestUrl");
        mCloudGatewayUrl = outState.getString("mCloudGatewayUrl");
        mSipUrl = outState.getString("mSipUrl");
        mTurnUrl = outState.getString("mTurnUrl");
        mTurnPort = outState.getInt("mTurnPort");
        mVsaasDomain = outState.getString("mVsaasDomain");
        mSipPassword = outState.getString("mSipPassword");
        mUuid = outState.getString("mUuid");
        mCustomerCtrtId = outState.getString("mCustomerCtrtId");
        mCustomerCustId = outState.getString("mCustomerCustId");
        mCustomerMemberId = outState.getString("mCustomerMemberId");
        mCustomerPhone = outState.getString("mCustomerPhone");
        mVer = outState.getString("mVer");
        sPush_Type_Mode_Chg_Yn = outState.getString("sPush_Type_Mode_Chg_Yn");
        sPush_Type_Prsnc_Chg_Yn = outState.getString("sPush_Type_Prsnc_Chg_Yn");
        sInvitations_Count = outState.getString("sInvitations_Count");
        mSelectHomehubDeviceId = outState.getString("mSelectHomehubDeviceId");
//        sSelect_Homegrp_Homehub_Online_State = outState.getString("sSelect_Homegrp_Homehub_Online_State");
//        sSelect_Homegrp_Homehub_Conn_State = outState.getString("sSelect_Homegrp_Homehub_Conn_State");
//        sSelect_Homegrp_Homehub_Dongle_State = outState.getString("sSelect_Homegrp_Homehub_Dongle_State");
//        sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime = outState.getString("sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime");
//        sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime = outState.getString("sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime");
        selectServicehubAppId = outState.getString("selectServicehubAppId");
        selectServicehubRequestId = outState.getInt("selectServicehubRequestId");
        mSelectHomehubDeviceKey = outState.getString("mSelectHomehubDeviceKey");
        mIsCtrtGroupHomehub = outState.getBoolean("mIsCtrtGroupHomehub");
        mCustId = outState.getString("mCustId");
        mQuiz = outState.getString("mQuiz");
        mPhone = outState.getString("mPhone");
        mCtrtGroupId = outState.getString("ctrtGroupId");
    }

    public String getInvitations_Count() {

        return sInvitations_Count;
    }

    public void setInvitations_Count(String str) {

        sInvitations_Count = str;
    }

    public String getSelectHomehubDeviceId() {

        return mSelectHomehubDeviceId;
    }

    public void setSelectHomehubDeviceId(String str) {

        mSelectHomehubDeviceId = str;
    }

//    public String getSelect_Homegrp_Homehub_Online_State() {
//
//        String onlineState = "";
//        if (sSelect_Homegrp_Homehub_Conn_State.equalsIgnoreCase("connect") &&
//                sSelect_Homegrp_Homehub_Dongle_State.equalsIgnoreCase("ON")) {
//            onlineState = "ON";
//        } else {
//            onlineState = "OFF";
//        }
//        return onlineState;
//    }

    public String getSelectCtrtGroupHomehubOnlineState() {

        String onlineState = "";
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ONLINE)).equals("ON")) {
                    if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_CONN)).equals("connect")) {
                        onlineState = "ON";
                    } else {
                        onlineState = "OFF";
                        break;
                    }
                } else {
                    onlineState = "OFF";
                    break;
                }
            }
        }
        return onlineState;
    }

    public String getSelectCtrtGroupFirstHomehubOnlineState() {
        return getSelectCtrtGroupHomehubOnlineState(getFirstHomehubDeviceId());
    }

    public String getSelectCtrtGroupHomehubOnlineState(String deviceId) {

        String onlineState = "";
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ID)).equals(deviceId)) {
                    if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ONLINE)).equals("ON")) {
                        if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_CONN)).equals("connect")) {
                            onlineState = "ON";
                            break;
                        } else {
                            onlineState = "OFF";
                            break;
                        }
                    } else {
                        onlineState = "OFF";
                        break;
                    }
                }
            }
        }
        return onlineState;
    }

    public int getHomehubTotalCtrtCnt(String deviceId) {
        int totalCtrtCnt = 0;
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ID)).equals(deviceId)) {
                    totalCtrtCnt += mUtil.null2int(hmTempDevice.get(Constants.KEY_TOTAL_CTRT_CNT));
                    break;
                }
            }
        }
        return totalCtrtCnt;
    }

    public int getHomehubTotalCtrtCnt() {
        int totalCtrtCnt = 0;
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                totalCtrtCnt += mUtil.null2int(hmTempDevice.get(Constants.KEY_TOTAL_CTRT_CNT));
            }
        }
        return totalCtrtCnt;
    }

    public int getHomehubRemainCtrtCnt(String deviceId) {
        int remainCtrtCnt = 0;
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ID)).equals(deviceId)) {
                    remainCtrtCnt += mUtil.null2int(hmTempDevice.get(Constants.KEY_REMAIN_CTRT_CNT));
                    break;
                }
            }
        }
        return remainCtrtCnt;
    }

    public int getHomehubRemainCtrtCnt() {
        int remainCtrtCnt = 0;
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                remainCtrtCnt += mUtil.null2int(hmTempDevice.get(Constants.KEY_REMAIN_CTRT_CNT));
            }
        }
        return remainCtrtCnt;
    }


    public String getHomehubDeviceName(String deviceId) {
        String deviceName = "";
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ID)).equals(deviceId)) {
                    deviceName = getHomehubDeviceName(i);
                    break;
                }
            }
        }
        return deviceName;
    }

    public String getHomehubDeviceName(int row) {
        String deviceName = "";
        if (mDevicesList.size() > 0) {
            HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
            hmTempDevice = mDevicesList.get(row);
            String prdName = mUtil.null2string(hmTempDevice.get(Constants.KEY_PRD_NAME));
            String prdUserName = mUtil.null2string(hmTempDevice.get(Constants.KEY_PRD_USER_NAME));
            deviceName = !prdUserName.equals("") ? prdUserName : prdName;
        }
        return deviceName;
    }
    public String getHomehubDeviceSn(int row) {
        String deviceSn = "";
        if (mDevicesList.size() > 0) {
            HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
            hmTempDevice = mDevicesList.get(row);
            deviceSn =  mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_SN));
        }
        return deviceSn;
    }
    public String getHomehubDeviceId(int row) {
        String deviceId = "";
        if (mDevicesList.size() > 0) {
            HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
            hmTempDevice = mDevicesList.get(row);
            deviceId = mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ID));
        }
        return deviceId;
    }

    public String getFirstHomehubDeviceName() {
        return getHomehubDeviceName(0);
    }

    public String getFirstHomehubDeviceId() {
        String deviceId = "";
        if (mDevicesList.size() > 0) {
            HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
            hmTempDevice = mDevicesList.get(0);
            deviceId =  mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ID));
        }
        return deviceId;
    }

    public String getSelectCtrtGroupHomehubOfflineStateDisplay() {

        String displayHomehubState = "";
        String firstHomehubName = "";
        int offCount = 0;
        Boolean isOffline = false;
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ONLINE)).equals("ON")) {
                    if (!mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_CONN)).equals("connect")) {
                        if (!isOffline) {
                            Dlog.v("jino111115");
                            isOffline = true;
                            String prdName = mUtil.null2string(hmTempDevice.get(Constants.KEY_PRD_USER_NAME));
                            String prdUserName = mUtil.null2string(hmTempDevice.get(Constants.KEY_PRD_USER_NAME));
                            firstHomehubName = !prdUserName.equals("") ? prdUserName : prdName;
                        } else {
                            isOffline = true;
                            Dlog.v("jino111116");
                            offCount++;
                        }
                    }
                } else {
                    if (!isOffline) {
                        isOffline = true;
                        Dlog.v("jino111117");
                        String prdName = mUtil.null2string(hmTempDevice.get(Constants.KEY_PRD_USER_NAME));
                        String prdUserName = mUtil.null2string(hmTempDevice.get(Constants.KEY_PRD_USER_NAME));
                        firstHomehubName = !prdUserName.equals("") ? prdUserName : prdName;
                    } else {
                        isOffline = true;
                        offCount++;
                    }
                }
            }
            if (isOffline) {
                Dlog.v("jino111118");
                if (offCount == 0) {
                    Dlog.v("jino one offline");
                    displayHomehubState = mUtil.getString(getApplicationContext(), R.string.layout_homehub_offline_display,
                            firstHomehubName);
                } else {
                    Dlog.v("jino multi offline");
                    displayHomehubState = mUtil.getString(getApplicationContext(), R.string.layout_homehub_offline_multi_display,
                            firstHomehubName, offCount + "");
                }
            }
        }
        return displayHomehubState;
    }


//    public String getSelect_Homegrp_Homehub_Online_State() {
//
//        String onlineState = "";
//        if (sSelect_Homegrp_Homehub_Conn_State.equalsIgnoreCase("connect") &&
//                sSelect_Homegrp_Homehub_Dongle_State.equalsIgnoreCase("ON")) {
//            onlineState = "ON";
//        } else {
//            onlineState = "OFF";
//        }
//        return onlineState;
//    }

//    public void setSelect_Homegrp_Homehub_Online_State(String str) {
//        LogUtil.i(TAG, "setSelect_Homegrp_Homehub_Online_State : " + str);
//        sSelect_Homegrp_Homehub_Online_State = str;
//    }

//    public String getsSelect_Homegrp_Homehub_Conn_State() {
//        return sSelect_Homegrp_Homehub_Conn_State;
//    }
//
//    public void setsSelect_Homegrp_Homehub_Conn_State(String sSelect_Homegrp_Homehub_Conn_State) {
//        LogUtil.i(TAG, "setsSelect_Homegrp_Homehub_Conn_State : " + sSelect_Homegrp_Homehub_Conn_State);
//        this.sSelect_Homegrp_Homehub_Conn_State = sSelect_Homegrp_Homehub_Conn_State;
//    }

//    public String getsSelect_Homegrp_Homehub_Dongle_State() {
//        return sSelect_Homegrp_Homehub_Dongle_State;
//    }
//
//    public void setsSelect_Homegrp_Homehub_Dongle_State(String sSelect_Homegrp_Homehub_Dongle_State) {
//        LogUtil.i(TAG, "setsSelect_Homegrp_Homehub_Dongle_State : " + sSelect_Homegrp_Homehub_Dongle_State);
//        this.sSelect_Homegrp_Homehub_Dongle_State = sSelect_Homegrp_Homehub_Dongle_State;
//    }

    public String getsSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime() {
        return sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime;
    }

    public void setsSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime(String sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime) {
        LogUtil.i(TAG, "setsSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime : " + sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime);
        this.sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime = sSelect_CtrtGroup_Homehub_Dongle_Online_Last_Datetime;
    }

    public String getsSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime() {
        return sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime;
    }

    public void setsSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime(String sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime) {
        LogUtil.i(TAG, "setsSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime : " + sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime);
        this.sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime = sSelect_CtrtGroup_Homehub_Mqtt_Online_Last_Datetime;
    }

    public String getPush_Type_Mode_Chg_Yn() {

        return sPush_Type_Mode_Chg_Yn;
    }

    public void setPush_Type_Mode_Chg_Yn(String str) {

        sPush_Type_Mode_Chg_Yn = str;
    }

    public String getPush_Type_Prsnc_Chg_Yn() {

        return sPush_Type_Prsnc_Chg_Yn;
    }

    public void setPush_Type_Prsnc_Chg_Yn(String str) {

        sPush_Type_Prsnc_Chg_Yn = str;
    }

    public String getTemp_Password_Yn() {

        return sTemp_Password_Yn;
    }

    public void setTemp_Password_Yn(String str) {

        sTemp_Password_Yn = str;
    }

    public String getCtrtGroupGradeCode() {

        return mCtrtGroupGradeCode;
    }

    public void setCtrtGroupGradeCode(String str) {

        mCtrtGroupGradeCode = str;
    }

    public String getSelect_Homegrp_Device_Yn() {

        return sSelect_CtrtGroup_Device_Yn;
    }

    public void setSelect_Homegrp_Device_Yn(String str) {

        sSelect_CtrtGroup_Device_Yn = str;
    }

    public String getCtrtGroupName() {

        return mCtrtGroupName;
    }

    public void setCtrtGroupName(String str) {

        mCtrtGroupName = str;
    }

//    public String getSelect_Homegrp_Icon() {
//
//        return sSelect_CtrtGroup_Icon;
//    }
//
//    public void setSelect_Homegrp_Icon(String str) {
//
//        sSelect_CtrtGroup_Icon = str;
//    }

    public String getSelect_Location_Addr() {

        return sSelect_Location_Addr;
    }

    public void setSelect_Location_Addr(String str) {

        sSelect_Location_Addr = str;
    }

    public String getSelect_Location_Coordinate() {

        return sSelect_Location_Coordinate;
    }

    public void setSelect_Location_Coordinate(String str) {

        sSelect_Location_Coordinate = str;
    }

    public String getSelect_Location_Radius() {

        return sSelect_Location_Radius;
    }

    public void setSelect_Location_Radius(String str) {

        sSelect_Location_Radius = str;
    }

    public String getLoginMemberId() {

        return mLoginMemberId;
    }

    public void setLoginMemberId(String str) {

        mLoginMemberId = str;
    }

    public String getCountry_Id() {

        return sCountry_Id;
    }

    public void setCountry_Id(String str) {

        sCountry_Id = str;
    }

    public String getLanguage_Id() {

        return sLanguage_Id;
    }

    public void setLanguage_Id(String str) {

        sLanguage_Id = str;
    }

    public String getService_Id() {

        return mServiceId;
    }

    public void setService_Id(String str) {

        mServiceId = str;
    }

    public String getProject_Id() {

        return mProjectId;
    }

    public void setProject_Id(String str) {

        mProjectId = str;
    }

    public String getAppstart_Id() {

        return mAppStartId;
    }

    public void setAppstart_Id(String str) {

        mAppStartId = str;
    }

    public String getUrl() {

        return mUrl;
    }

    public void setUrl(String str) {

        mUrl = str;
    }

    public String getSmartUrl() {

        return mSmartUrl;
    }

    public void setSmartUrl(String str) {

        mSmartUrl = str;
    }

    public String getServicehubWebsocketUrl() {

        return mServicehubWebsocketUrl;
    }

    public void setServicehubWebsocketUrl(String str) {

        mServicehubWebsocketUrl = str;
    }

    public String getServicehubRestUrl() {
        return mServicehubRestUrl;
    }

    public void setServicehubRestUrl(String sServicehubRestUrl) {
        this.mServicehubRestUrl = sServicehubRestUrl;
    }

    public String getCloudGatewayUrl() {
        return mCloudGatewayUrl;
    }

    public void setCloudGatewayUrl(String sCloudGatewayUrl) {
        this.mCloudGatewayUrl = sCloudGatewayUrl;
    }

    public String getSipUrl() {
        return mSipUrl;
    }

    public void setSipUrl(String sipUrl) {
        this.mSipUrl = sipUrl;
    }

    public String getTurnUrl() {
        return mTurnUrl;
    }

    public void setTurnUrl(String turnUrl) {
        this.mTurnUrl = turnUrl;
    }

    public int getTurnPort() {
        return mTurnPort;
    }

    public void setTurnPort(int turnPort) {
        this.mTurnPort = turnPort;
    }

    public String getVsaasDomain() {
        return mVsaasDomain;
    }

    public void setVsaasDomain(String vsaasDomain) {
        this.mVsaasDomain = vsaasDomain;
    }

    public String getSipPassword() {
        return mSipPassword;
    }

    public void setSipPassword(String sipPassword) {
        this.mSipPassword = sipPassword;
    }

    public String getAuthToken() {

        return mAuthToken;
    }

    public void setAuthToken(String str) {

        mAuthToken = str;
    }

    public String getAuthorization() {

        if (!"".equals(mLoginId)) {
            LogUtil.i(TAG, "Authorization - " + Base64.encodeToString((mLoginId + ":" + getAuthToken()).getBytes(), 0));
            return Base64.encodeToString((mLoginId + ":" + getAuthToken()).getBytes(), 0).replaceAll("\\s", "");
        }
        return null;
    }

    public Float getScale() {

        return mScale;
    }

    public void setScale(Float str) {

        mScale = str;
    }

    public int getDisplayHeight() {

        return mDisplayHeight;
    }

    public void setDisplayHeight(int str) {

        mDisplayHeight = str;
    }

    public int getDisplayWidth() {

        return mDisplayWidth;
    }

    public void setDisplayWidth(int str) {

        mDisplayWidth = str;
    }

    public String getLogin_Pass() {

        return mLoginPass;
    }

    public void setLogin_Pass(String str) {

        mLoginPass = str;
    }

    public String getPush_Token() {

        return mPushToken;
    }

    public void setPush_Token(String str) {

        mPushToken = str;
    }

    public String getMyImageUrl() {

        return mMyImageUrl;
    }

    public void setMyImageUrl(String str) {

        mMyImageUrl = str;
    }

    public String getNickname() {

        return mNickname;
    }

    public void setNickname(String str) {

        mNickname = str;
    }

    public String getTimezone_Id() {

        return mTimezoneId;
    }

    public void setTimezone_Id(String str) {

        mTimezoneId = str;
    }

    public Boolean getGoErr() {

        return mIsGoErr;
    }

    public void setGoErr(Boolean sVal) {

        mIsGoErr = sVal;
    }

    public String getCustomerCtrtId() {

        return mCustomerCtrtId;
    }

    public void setCustomerCtrtId(String str) {

        mCustomerCtrtId = str;
    }

//    public String getCustomerCustId() {
//
//        return mCustomerCustId;
//    }

//    public void setCustomerCustId(String str) {
//
//        mCustomerCustId = str;
//    }
//
    public String getCustomerMemberId() {

        return mCustomerMemberId;
    }

    public void setCustomerMemberId(String str) {

        mCustomerMemberId = str;
    }

    public String getCustomerPhone() {

        return mCustomerPhone;
    }

    public void setCustomerPhone(String str) {

        mCustomerPhone = str;
    }

    public String getVer() {

        return mVer;
    }

    public void setVer(String str) {

        mVer = str;
    }

    public String getUuid() {

        return mUuid;
    }

    public void setUuid(String str) {

        mUuid = str;
    }

    public Boolean getIsLogin() {

        return mIsLogin;
    }

    public void setIsLogin(Boolean str) {

        mIsLogin = str;
    }

    public Boolean getLogout() {
        return mIsLogout;
    }

    public void setLogout(Boolean logout) {
        mIsLogout = logout;
    }

    public Boolean getIntro() {

        return mIsIntro;
    }

    public void setIntro(Boolean str) {

        mIsIntro = str;
    }

    public Boolean getMain() {

        return mIsMain;
    }

    public void setMain(Boolean str) {

        mIsMain = str;
    }

    public Boolean getIsWebSocketAuth() {
        return mIsWebSocketAuth;
    }

    public void setIsWebSocketAuth(Boolean isWebSocketAuth) {
        this.mIsWebSocketAuth = isWebSocketAuth;
    }

    public Boolean getIsWebSocketConn() {
        return mIsWebSocketConn;
    }

    public void setIsWebSocketConn(Boolean isWebSocketConn) {
        this.mIsWebSocketConn = isWebSocketConn;
    }

    public Boolean getIsCustomerConn() {
        return mIsCustomerConn;
    }

    public void setIsCustomerConn(Boolean isCustomerConn) {
        this.mIsCustomerConn = isCustomerConn;
    }

    public Boolean getIsAutoLogin() {

        return mIsAutoLogin;
    }

    public void setIsAutoLogin(Boolean str) {

        mIsAutoLogin = str;
    }

    public Boolean getSaveId() {
        return mSaveId;
    }

    public void setSaveId(Boolean saveId) {
        this.mSaveId = saveId;
    }

    public Boolean getManualNotView() {

        return mIsManualNotView;
    }

    public void setManualNotView(Boolean str) {

        mIsManualNotView = str;
    }

    public Boolean getPush() {

        return mIsPush;
    }

    public void setPush(Boolean str) {

        mIsPush = str;
    }

    public String getLoginId() {

        if (mLoginId == null) {
            return "";
        } else {
            return mLoginId;
        }
    }

    public void setLoginId(String str) {

        mLoginId = str;
    }

    public String getLogin_TermId() {

        return mLoginTermId;
    }

    /**
     * 나의 홈 그룹 리스트 *
     */
    public ArrayList<HashMap<String, Object>> getCtrtGrpList() {

        return mCtrtgrpList;
    }

    /**
     * 나의 홈 그룹 리스트 *
     */
    public void setCtrtGrpList(ArrayList<HashMap<String, Object>> hm) {

        mCtrtgrpList = hm;
    }

    /**
     * 나의 디바이스 리스트 *
     */
    public ArrayList<HashMap<String, Object>> getDevicesList() {

        return mDevicesList;
    }

    /**
     * 나의 디바이스 리스트 *
     */
    public void setDevicesList(ArrayList<HashMap<String, Object>> hm) {

        mDevicesList = hm;
    }

    /**
     * 나의 디바이스 리스트 *
     */
    public HashMap<String, Object> getDevicesList(String deviceId) {
        HashMap<String, Object> hmDevice = new HashMap<String, Object>();
        if (mDevicesList.size() > 0) {
            for (int i = 0; i < mDevicesList.size(); i++) {
                HashMap<String, Object> hmTempDevice = new HashMap<String, Object>();
                hmTempDevice = mDevicesList.get(i);
                if (mUtil.null2string(hmTempDevice.get(Constants.KEY_DEVICE_ID)).equals(deviceId)) {
                    hmDevice = mDevicesList.get(i);
                }
            }
        }
        return hmDevice;
    }

    public ArrayList<HashMap<String, Object>> getInvitationList() {

        return mInvitationList;
    }

    public void setInvitationList(ArrayList<HashMap<String, Object>> hm) {

        mInvitationList = hm;
    }

    public String getSelectServicehubAppId() {
        return selectServicehubAppId;
    }

    public void setSelectServicehubAppId(String selectServicehubAppId) {
        this.selectServicehubAppId = selectServicehubAppId;
    }

    public String getSelectHomehubDeviceKey() {
        return mSelectHomehubDeviceKey;
    }

    public void setSelectHomehubDeviceKey(String str) {
        this.mSelectHomehubDeviceKey = str;
    }

    public Boolean getIsCtrtGroupHomehub() {
        return mIsCtrtGroupHomehub;
    }

    public void setIsCtrtGroupHomehub(Boolean isCtrtGroupHomehub) {
        this.mIsCtrtGroupHomehub = isCtrtGroupHomehub;
    }

    public String getCustId() {
        return mCustId;
    }

    public void setCustId(String custId) {
        this.mCustId = custId;
    }

    public String getQuiz() {
        return mQuiz;
    }

    public void setQuiz(String custId) {
        this.mQuiz = custId;
    }

    public String getPhone() {
        return mPhone;
    }

    public void setPhone(String phone) {
        mPhone = phone;
    }

    public String getCtrtGroupId() {

        return mCtrtGroupId;
    }

    public void setCtrtGroupId(String ctrtGroupId) {
        this.mCtrtGroupId = ctrtGroupId;
    }

    public String getMasterTransferPopupYn() {
        return mMasterTransferPopupYn;
    }

    public void setMasterTransferPopupYn(String masterTransferPopupYn) {
        mMasterTransferPopupYn = masterTransferPopupYn;
    }

    /**
     * Websocket request id 생성
     *
     * @return request id
     */
    public String getSelectServicehubRequestId() {
        return String.valueOf(selectServicehubRequestId++ % 255);
    }


    public enum TrackerName {
        APP_TRACKER,
        // 앱 별로 트래킹
        GLOBAL_TRACKER,
        // 모든 앱을 통틀어 트래킹
        ECOMMERCE_TRACKER,     // 아마 유료 결재 트래킹 개념 같음
    }

}
