package com.app.simplitend.caregiverdashboard.activities.deactivateacc;

import androidx.annotation.NonNull;

import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.caregiverdashboard.mvvm.CgHomeContracts;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;

import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AccountPresenter {

    public static final int ACC_ACTIVE = 1;
    public static final String ACC_ACTIVE_STR = "1";
    public static final int ACC_DEACTIVATE_BY_USER = 2;
    public static final int ACC_DEACTIVATE_BY_ADMIN = 3;
    public static final String DEFAULT_ACTIVE_STATUS = "0";

    private static AccountPresenter accountPresenter;

    private AccountApiService apiService;

    private AccountPresenter(){
        apiService = RetrofitHelper.getRetrofit().create(AccountApiService.class);
    }

    public synchronized static AccountPresenter getAccountPresenter(){
        if (accountPresenter == null){
            accountPresenter = new AccountPresenter();
        }

        return accountPresenter;
    }

    public void accountReDeActivate(int active_status,
                                    int caregiver_xid,
                                    String reason,
                                    String comment,
                                    @NonNull String token,
                                    @NonNull AccountReDeActivateCallback callback){

        Map<String, String> body = new HashMap<>();

        body.put("status", active_status + "");
        if (reason != null) body.put("reason", reason);
        if (comment != null) body.put("description", comment);

        apiService.de_re_ActivateAccount(body, caregiver_xid, token)
                .enqueue(new Callback<CallResponse<Object>>() {
                    @Override
                    public void onResponse(Call<CallResponse<Object>> call, Response<CallResponse<Object>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().error_code != 0){
                                callback.onAccountDeReActivateFailed(new Exception(), response.body().message);
                                return;
                            }

                            callback.onAccountDeReActivate();
                        }else{
                            callback.onAccountDeReActivateFailed(new Exception(), "Couldn't complete that action.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<Object>> call, Throwable t) {
                        callback.onAccountDeReActivateFailed(new Exception(), "Couldn't complete that action.");
                    }
                });

    }

    public void contactAdmin(Map<String, RequestBody> body,
                             @NonNull String token, @NonNull ContactAdminCallback contactAdminCallback){

        apiService.contactAdmin(body, token)
                .enqueue(new Callback<CallResponse<Object>>() {
                    @Override
                    public void onResponse(Call<CallResponse<Object>> call, Response<CallResponse<Object>> response) {
                        if (response.body() != null){
                            if (response.body().status != 200 || response.body().error_code != 0){
                                contactAdminCallback.onFailed(response.body().message);
                                return;
                            }

                            contactAdminCallback.onSuccess();
                        }else{
                            contactAdminCallback.onFailed("Couldn't connect. Please try again later.");
                        }
                    }

                    @Override
                    public void onFailure(Call<CallResponse<Object>> call, Throwable t) {
                        contactAdminCallback.onFailed("Couldn't connect. Please try again later.");
                    }
                });

    }

    public void signOut(String token, @NonNull CgHomeContracts.SignOutCallback signOutCallback) {
        apiService.signOut(token).enqueue(new Callback<CallResponse<Object>>() {
            @Override
            public void onResponse(Call<CallResponse<Object>> call, Response<CallResponse<Object>> response) {
                if (response.body() != null) {
                    if (response.body().status != 200) {
                        signOutCallback.onSignOutFailed(new Exception(), response.body().message);
                        return;
                    }

                    signOutCallback.onSignOutSuccess();
                } else {
                    signOutCallback.onSignOutFailed(new Exception(), "Please try again later.");
                }
            }

            @Override
            public void onFailure(Call<CallResponse<Object>> call, Throwable t) {
                signOutCallback.onSignOutFailed(new Exception(), "Please try again later.");
            }
        });
    }

    public interface AccountReDeActivateCallback{
        void onAccountDeReActivate();

        void onAccountDeReActivateFailed(Throwable throwable, String message);
    }

    public interface ContactAdminCallback{
        void onSuccess();

        void onFailed(String message);
    }



}
