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

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

import kr.co.hanwha.hifive.admin.network.AdminNetworkUtil;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberDetailRoot;
import kr.co.hanwha.hifive.data.model.BaseData;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;

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

/**
 * Created by sanghoonkwon on 2018. 3. 12..
 */

public class AdminDataRepostiory implements AdminDataSource {
    private static AdminDataRepostiory sInstance = null;

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

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

    @Override
    public void clearData() {
    }

    @Override
    public void getMemberDetail(@NonNull Context context, @NonNull int member_id, @NonNull final HanwhaNetworkCallback callback) {
        if (isNetworkConnected(context) == false) {
            callback.onFailure(new HanwhaNetworkError(HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR));
            return;
        }

        AdminNetworkUtil networkUtil = new AdminNetworkUtil();
        networkUtil.requestMemberDetail(context, member_id, new HanwhaNetworkCallback<AdminMemberDetailRoot>() {
            @Override
            public void onSuccess(AdminMemberDetailRoot response) {
                callback.onSuccess(response);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                if (error != null && error.getStatusCode() == -2) {
                    callback.onSuccess(null);
                } else
                    callback.onFailure(error);
            }

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

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

    @Override
    public void requestResetPassword(@NonNull Context context, @NonNull int member_id, @NonNull String password, @NonNull final HanwhaNetworkCallback callback) {
        if (isNetworkConnected(context) == false) {
            callback.onFailure(new HanwhaNetworkError(HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR));
            return;
        }

        AdminNetworkUtil networkUtil = new AdminNetworkUtil();
        networkUtil.requestMemberResetPassword(context, member_id, password, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                callback.onSuccess(response);
            }

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

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

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

    @Override
    public void requestResetSimplePassword(@NonNull Context context, @NonNull int member_id, @NonNull final HanwhaNetworkCallback callback) {
        if (isNetworkConnected(context) == false) {
            callback.onFailure(new HanwhaNetworkError(HanwhaNetworkError.CODE_NETWORK_CONNECTION_ERROR));
            return;
        }

        AdminNetworkUtil networkUtil = new AdminNetworkUtil();
        networkUtil.requestMemberResetSimplePassword(context, member_id, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                callback.onSuccess(response);
            }

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

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

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