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

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

import com.google.firebase.iid.FirebaseInstanceId;

import java.util.ArrayList;

import kr.co.hanwha.hifive.data.model.BaseData;
import kr.co.hanwha.hifive.data.model.CrudDataRoot;
import kr.co.hanwha.hifive.data.model.LoginData;
import kr.co.hanwha.hifive.data.model.ProjectInfoData;
import kr.co.hanwha.hifive.data.model.UpdateData;
import kr.co.hanwha.hifive.data.source.contract.MemberDataSource;
import kr.co.hanwha.hifive.enums.RollCode;
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;

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

public class MemberDataRepository implements MemberDataSource {
    private static MemberDataRepository sInstance = null;
    private static UpdateData sUpdateData = 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() {
        sUpdateData = null;
        sLoginData = null;
    }

    public LoginData getLoginData() {
        return sLoginData;
    }

    public static UpdateData getUpdateData() {
        return sUpdateData;
    }

    public static void setUpdateData(UpdateData updateData) {
        sUpdateData = updateData;
    }

    public static String getLastVersion() {
        if (sUpdateData != null && sUpdateData.getUpdateInfo() != null) {
            return sUpdateData.getUpdateInfo().getVersion();
        }
        return "";
    }

    public static String getUpdateUrl() {
        if (sUpdateData != null && sUpdateData.getUpdateInfo() != null) {
            return sUpdateData.getUpdateInfo().getUpdate_url();
        }
        return "";
    }

    @Override
    public boolean isMember(int member_id) {
        return (member_id == getMemberId());
    }

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

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

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

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

    @Override
    public boolean isCaptain() {
        boolean isCaptain = false;
        if (sLoginData != null && sLoginData.getListMyProjectsInfo() != null) {
            for (ProjectInfoData data : sLoginData.getListMyProjectsInfo()) {
                if (data.getRole_code() == RollCode.CAPTAIN.getCode()) {
                    isCaptain = true;
                }
            }
        }
        return isCaptain;
    }

    @Override
    public ArrayList<ProjectInfoData> getCaptainProjectInfo() {
        ArrayList<ProjectInfoData> list = new ArrayList<>();

        if (sLoginData != null && sLoginData.getListMyProjectsInfo() != null) {
            for (ProjectInfoData data : sLoginData.getListMyProjectsInfo()) {
                if (data.getRole_code() == RollCode.CAPTAIN.getCode()) {
                    list.add(data);
                }
            }
        }

        return list;
    }

    @Override
    public boolean checkProjectId(@NonNull int project_id) {
        if (sLoginData != null && sLoginData.getListMyProjectsInfo() != null) {
            for (int i = 0; i < sLoginData.getListMyProjectsInfo().size(); i++) {
                if (project_id == sLoginData.getListMyProjectsInfo().get(i).getProject_id())
                    return true;
            }
        }
        return false;
    }

    @Override
    public ArrayList<Integer> getMyProjectList() {
        ArrayList<Integer> list = new ArrayList<>();
        if (sLoginData != null && sLoginData.getListMyProjectsInfo() != null) {
            for (int i = 0; i < sLoginData.getListMyProjectsInfo().size(); i++) {
                list.add(sLoginData.getListMyProjectsInfo().get(i).getProject_id());
            }
        }
        return list;
    }

    @Override
    public void doAccountLogin(@NonNull final Context context,
                               @NonNull String login, @NonNull String password,
                               @NonNull final HanwhaNetworkCallback<LoginData> callback) {
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestMembersLogin(context, login, password, new HanwhaNetworkCallback<LoginData>() {
            @Override
            public void onSuccess(LoginData response) {
                doSuccessLogin(context, response, 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 final Context context, @NonNull String login_id, @NonNull String authcode, @NonNull final HanwhaNetworkCallback callback) {
        String encAuthcode = HFCryptoUtil.getEncAuthcodeSHA256(login_id, authcode);

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

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

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

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

    @Override
    public void doSuccessLogin(@NonNull Context context, @Nullable LoginData response, @NonNull HanwhaNetworkCallback<LoginData> callback) {
        sLoginData = response;

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

    @Override
    public void updateDeviceToken(@NonNull Context context, @NonNull String token, @NonNull String device_uid, @NonNull final HanwhaNetworkCallback callback) {
        String encryption_member_id = getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestMemberDeviceAdd(context, encryption_member_id, token, device_uid, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                callback.onSuccess(sLoginData);
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                callback.onSuccess(sLoginData);
                //TODO Device 등록 실패시 처리 로직 고민 필요
            }

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

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

    @Override
    public void updateAuthcode(@NonNull Context context, @NonNull String authcode, @NonNull final HanwhaNetworkCallback callback) {
        String login_id = sLoginData.getLogin();
        String encryption_member_id = getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestMembersUpdateAuthcode(context, login_id, authcode, encryption_member_id, new HanwhaNetworkCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData response) {
                callback.onSuccess(null);
            }

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

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

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

    @Override
    public void getNotificationStatus(@NonNull Context context, @NonNull final HanwhaNetworkCallback<CrudDataRoot> callback) {
        String encryption_member_id = getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestNotificationStatus(context, encryption_member_id, callback);
    }

    @Override
    public void setNotificationStatus(@NonNull Context context, @NonNull boolean onoff, @NonNull HanwhaNetworkCallback<CrudDataRoot> callback) {
        String encryption_member_id = getEncryptionMemberId();
        HFNetworkUtil networkUtil = new HFNetworkUtil();
        networkUtil.requestNotificationSet(context, encryption_member_id, onoff, callback);
    }
}
