/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.taverna.mobile.ui.playerlogin;

import android.util.Base64;
import android.util.Log;

import org.apache.taverna.mobile.data.DataManager;
import org.apache.taverna.mobile.ui.base.BasePresenter;

import okhttp3.ResponseBody;
import retrofit2.adapter.rxjava.HttpException;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class PlayerLoginPresenter extends BasePresenter<PlayerLoginMvpView> {

    private DataManager mDataManager;

    private Subscription mSubscriptions;

    public PlayerLoginPresenter(DataManager dataManager) {
        mDataManager = dataManager;
    }

    @Override
    public void attachView(PlayerLoginMvpView mvpView) {
        super.attachView(mvpView);
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscriptions != null) mSubscriptions.unsubscribe();
    }

    public void playerLogin(final String username, final String password, final boolean loginFlag) {
        if (mSubscriptions != null) mSubscriptions.unsubscribe();

        mSubscriptions = mDataManager.authPlayerUserLoginDetail(getEncodedCredential(username,
                password), loginFlag)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("TAG", "onError: ", e);
                        if (e instanceof HttpException) {
                            if (((HttpException) e).code() == 401) {
                                getMvpView().showCredentialError();
                            } else if (((HttpException) e).code() == 406) {
                                getMvpView().validCredential();
                                mDataManager.getPreferencesHelper().setUserPlayerLoggedInFlag
                                        (loginFlag, getEncodedCredential(username, password));

                            }
                        }
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        Log.e("hello", "onCompleted: " + responseBody.byteStream());
                    }
                });

    }

    private String getEncodedCredential(String username, String password) {

        return "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64
                .NO_WRAP);
    }
}

@@ -1292,24 +1292,107 @@
 MvpView%3E %7B%0A%0A
+    private static final String TAG = PlayerLoginPresenter.class.getSimpleName();%0A%0A
     private 
@@ -2506,13 +2506,11 @@
 g.e(
-%22
 TAG
-%22
 , %22o
@@ -3045,24 +3045,133 @@
 assword));%0A%0A
+                            %7D else %7B%0A                                getMvpView().showError(%22Server Error%22);%0A
             
@@ -3369,15 +3369,11 @@
 g.e(
-%22hello%22
+TAG
 , %22o

add log tag