package ai.pai.lensman.login;

import android.content.Context;

import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import ai.pai.lensman.base.BaseInteractor;
import ai.pai.lensman.db.Preferences;

/**
 * Created by chengzhenyu on 2016/8/11.
 */
public class LoginPresenter implements LoginContract.Presenter,BaseInteractor.InteractorListener<String> {

    private Context context;
    private LoginContract.View view;
    private LoginInteractor interactor;
    private static final String APP_ID = "";

    public LoginPresenter(Context context,LoginContract.View view){
        this.view = view;
        this.context = context;
    }

    @Override
    public void login() {
        view.showProgressView();
        interactor = new LoginInteractor(this);
        interactor.startJob();
    }

    @Override
    public void start() {
        IWXAPI api = WXAPIFactory.createWXAPI(context, APP_ID, true);
        api.registerApp(APP_ID);
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "paiai_for_lensman";
        api.sendReq(req);
    }

    @Override
    public void stop() {
        if(interactor!=null){
            interactor.cancelJob();
        }
    }


    @Override
    public void onInteractSuccess(String result) {
        view.hideProgressView();
        Preferences.getInstance(context).setLensManId(result);
        view.jumpMain();
    }

    @Override
    public void onInteractFail(String errorMsg) {
        view.hideProgressView();
        view.showLoginHint(errorMsg);
    }

}
