package kr.co.hanwha.hifive.screen.intro;

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

import kr.co.hanwha.hifive.data.source.MemberDataRepository;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.screen.intro.contract.AuthNumberContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;

/**
 * Created by sanghoonkwon on 2018. 2. 7..
 */

public class AuthNumberPresenter implements AuthNumberContract.Presenter {
    private AuthNumberContract.View mView;

    public AuthNumberPresenter(@NonNull AuthNumberContract.View view) {
        mView = view;
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public Context getContext() {
        return (Context) mView;
    }

    @Override
    public void doUpdateAuthcode(@NonNull String authcode) {
        MemberDataRepository.getInstance().updateAuthcode(getContext(), authcode, new HanwhaNetworkCallback() {
            @Override
            public void onSuccess(Object response) {
                getActivity().setResult(Activity.RESULT_OK);
                getActivity().finish();
            }

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

            @Override
            public void onPreExecute() {
                mView.showLoadingDialog(true);
            }

            @Override
            public void onPostExecute() {
                mView.showLoadingDialog(false);
            }
        });
    }

    private Activity getActivity() {
        return (Activity) mView;
    }
}
