package com.example.audiovisualrecord.ui.login;

import android.content.Context;
import android.util.Log;

import com.example.audiovisualrecord.R;
import com.example.audiovisualrecord.utils.api.ApiService;
import com.example.audiovisualrecord.utils.api.HttpMethods;
import com.example.audiovisualrecord.utils.api.apidata.login.LoginRequest;
import com.example.audiovisualrecord.utils.api.apidata.login.LoginResponse;
import com.example.audiovisualrecord.utils.rxjava.SchedulerProvider;
import com.example.audiovisualrecord.utils.rxjava.SchedulerProviderImp;
import com.example.audiovisualrecord.utils.sharepreferences.LoginPreferences;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;

public class LoginPresenter<V extends LoginContract.View> implements LoginContract.Presenter<V> {

//    LoginPreferences loginPreferences;
    private V view;
    private String LOGIN_AUTHORIZED_ID = "06cdae9b-fa20-497d-8207-0eb73582623b";
    private ApiService api;
    SchedulerProviderImp schedulerProviderImp = new SchedulerProviderImp();
    private CompositeDisposable compositeDisposable;

    public LoginPresenter(V view) {
        this.view = view;
        api = HttpMethods.getInstance().getApi();
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void onLogin(String account, String password) {
        String url = view.getResourceString(R.string.api_on_Login);
//        LoginRequest mLoginRequest = new LoginRequest(LOGIN_AUTHORIZED_ID, "N000158385", "781122222");
        LoginRequest mLoginRequest = new LoginRequest(LOGIN_AUTHORIZED_ID, account, password);
        compositeDisposable.add(api.onLogin(url, mLoginRequest)
                .subscribeOn(schedulerProviderImp.io())
                .observeOn(schedulerProviderImp.ui())
                .subscribeWith(new DisposableObserver<LoginResponse>() {
                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        if ("True".equals(loginResponse.getmResult())) {
                            view.onCompleteLogin(loginResponse.getmMsg());
                        } else {

                            view.dismissProgressDialog();
                            view.showDialogCaveatMessage("登入失敗");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("err", e.getMessage());
                        view.dismissProgressDialog();
                        if(e.getMessage().contains("No address associated with hostname"))
                        {
                            view.showDialogCaveatMessage("請檢查網路狀態");
                        }
                        view.showDialogCaveatMessage("登入失敗");
                        view.setButtonEnable(true);
                    }

                    @Override
                    public void onComplete() {
                    }
                })
        );
    }

    @Override
    public void onAttached(V view) {

    }

    @Override
    public void onDetached() {

    }
}
