/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * Licensed 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 com.pyamsoft.pydroid.ads;

import android.support.annotation.NonNull;
import com.pyamsoft.pydroid.presenter.PresenterBase;
import com.pyamsoft.pydroid.social.SocialMediaPresenter;
import com.pyamsoft.pydroid.tool.ExecutedOffloader;
import timber.log.Timber;

class AdvertisementPresenterImpl extends PresenterBase<AdvertisementPresenter.AdView>
    implements AdvertisementPresenter {

  @SuppressWarnings("WeakerAccess") @NonNull final SocialMediaPresenter socialMediaPresenter;
  @NonNull private final AdvertisementInteractor interactor;
  @NonNull private ExecutedOffloader offloader = new ExecutedOffloader.Empty();

  AdvertisementPresenterImpl(@NonNull AdvertisementInteractor interactor,
      @NonNull SocialMediaPresenter socialMediaPresenter) {
    this.socialMediaPresenter = socialMediaPresenter;
    this.interactor = interactor;
  }

  @Override protected void onBind() {
    super.onBind();
    getView(socialMediaPresenter::bindView);
    showAd();
  }

  @SuppressWarnings("WeakerAccess") void unsubAdSubscription() {
    if (!offloader.isCancelled()) {
      offloader.cancel();
    }
  }

  @Override protected void onUnbind() {
    super.onUnbind();
    socialMediaPresenter.unbindView();
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    socialMediaPresenter.destroy();

    hideAd();
  }

  @Override public void showAd() {
    unsubAdSubscription();
    offloader = interactor.showAdView()
        .onError(item -> Timber.e(item, "onError showAd"))
        .onResult(item -> getView(AdView::onShown))
        .execute();
  }

  @Override public void hideAd() {
    unsubAdSubscription();
    offloader = interactor.hideAdView()
        .onError(item -> Timber.e(item, "onError hideAd"))
        .onResult(item -> getView(AdView::onHidden))
        .execute();
  }

  @Override public void clickAd(@NonNull String packageName) {
    socialMediaPresenter.clickAppPage(packageName);
  }
}

@@ -2112,36 +2112,37 @@
       .onResult(
-item
+shown
  -%3E getView(AdVi
@@ -2141,23 +2141,90 @@
 iew(
-AdView::onShown
+view -%3E %7B%0A          if (shown) %7B%0A            view.onShown();%0A          %7D%0A        %7D
 ))%0A 
@@ -2426,20 +2426,20 @@
 nResult(
-item
+hide
  -%3E getV
@@ -2446,24 +2446,65 @@
 iew(
-AdView::
+view -%3E %7B%0A          if (hide) %7B%0A            view.
 onHidden
 ))%0A 
@@ -2499,16 +2499,41 @@
 onHidden
+();%0A          %7D%0A        %7D
 ))%0A     

Fix process results, again.