package kr.co.hanwha.hifive.data.source;

import android.content.Context;
import android.support.annotation.NonNull;

import kr.co.hanwha.hifive.data.model.LoginData;
import kr.co.hanwha.hifive.data.source.contract.MemberDataSource;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.util.HFCommonUtil;
import kr.co.hanwha.hifive.util.HFCryptoUtil;

import static kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkUtil.isNetworkConnected;

/**
 * Created by sanghoonkwon on 2018. 1. 11..
 */

public class MemberDataRepository implements MemberDataSource {
    private static MemberDataRepository sInstance = null;
    private static LoginData sLoginData = null;

    public static MemberDataRepository getInstance() {
        if (sInstance == null) {
            sInstance = new MemberDataRepository();
        }
        return sInstance;
    }

    public static void destroyInstance() {
        sInstance = null;
    }

    @Override
    public void clearData() {
        sLoginData = null;
    }

    public LoginData getLoginData() {
        return sLoginData;
    }

    @Override
    public int getMemberId() {
        return (sLoginData != null ? sLoginData.getMember_id() : 0);
    }

    @Override
    public String getMemberName() {
        return (sLoginData != null ? sLoginData.getName() : "");
    }

    @Override
    public String getEncryptionMemberId() {
        return (sLoginData != null ? sLoginData.getEncryption_member_id() : "");
    }

    @Override
    public void doLogin(@NonNull final Context context,
                        @NonNull String login, @NonNull String password,
                        @NonNull final HanwhaNetworkCallback<LoginData> callback) {
        if (isNetworkConnected(context) == false) {
            callback.onFailure(new HanwhaNetworkError(HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR));
            return;
        }

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestMembersLogin(context, login, password, new HanwhaNetworkCallback<LoginData>() {
            @Override
            public void onSuccess(LoginData response) {
                sLoginData = response;
                callback.onSuccess(sLoginData);

                //TODO - ADD !!!!! CHECK!!!!!!!!!
//                String uuid = HFCommonUtil.getDeviceUUID(context);
//                String token = FirebaseInstanceId.getInstance().getToken();
//                updateDeviceToken(context, token, uuid, callback);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                callback.onFailure(error);
            }

            @Override
            public void onPreExecute() {
                callback.onPreExecute();
            }

            @Override
            public void onPostExecute() {
                callback.onPostExecute();
            }
        });

    }

    @Override
    public void doSimpleLogin(@NonNull Context context, @NonNull String login_id, @NonNull String authcode, @NonNull final HanwhaNetworkCallback callback) {
        if (isNetworkConnected(context) == false) {
            callback.onFailure(new HanwhaNetworkError(HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR));
            return;
        }

        String encAuthcode = HFCryptoUtil.getSHA256EncryptionData(login_id + "__newgw1!~" + authcode);//TODO - MOVE!!!

        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestMembersSimpleLogin(context, login_id, encAuthcode, new HanwhaNetworkCallback<LoginData>() {
            @Override
            public void onSuccess(LoginData response) {
                sLoginData = response;

                callback.onSuccess(sLoginData);

//                String uuid = HFCommonUtil.getDeviceUUID(context);
//                String token = FirebaseInstanceId.getInstance().getToken();
//                updateDeviceToken(context, token, uuid, callback);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                callback.onFailure(error);
            }

            @Override
            public void onPreExecute() {
                callback.onPreExecute();
            }

            @Override
            public void onPostExecute() {
                callback.onPostExecute();
            }
        });
    }
}
