/*
 * 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.powermanager.trigger;

import android.database.sqlite.SQLiteConstraintException;
import android.support.annotation.NonNull;
import com.pyamsoft.powermanager.trigger.bus.TriggerCreateEvent;
import com.pyamsoft.powermanager.trigger.bus.TriggerDeleteEvent;
import com.pyamsoft.powermanager.trigger.db.PowerTriggerEntry;
import com.pyamsoft.pydroid.bus.EventBus;
import com.pyamsoft.pydroid.helper.DisposableHelper;
import com.pyamsoft.pydroid.presenter.SchedulerPresenter;
import io.reactivex.Scheduler;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import javax.inject.Inject;
import javax.inject.Named;
import timber.log.Timber;

class TriggerPresenter extends SchedulerPresenter {

  @NonNull private final TriggerInteractor interactor;
  @NonNull private Disposable deleteDisposable = Disposables.empty();
  @NonNull private Disposable viewDisposable = Disposables.empty();
  @NonNull private Disposable createDisposable = Disposables.empty();
  @NonNull private Disposable createBus = Disposables.empty();
  @NonNull private Disposable deleteBus = Disposables.empty();

  @Inject TriggerPresenter(@NonNull @Named("obs") Scheduler obsScheduler,
      @NonNull @Named("sub") Scheduler subScheduler, @NonNull TriggerInteractor interactor) {
    super(obsScheduler, subScheduler);
    this.interactor = interactor;
  }

  @Override protected void onStop() {
    super.onStop();
    deleteDisposable = DisposableHelper.dispose(deleteDisposable);
    createDisposable = DisposableHelper.dispose(createDisposable);
    viewDisposable = DisposableHelper.dispose(viewDisposable);
    createBus = DisposableHelper.dispose(createBus);
    deleteBus = DisposableHelper.dispose(deleteBus);
  }

  /**
   * public
   */
  void registerOnBus(@NonNull BusCallback callback) {
    createBus = DisposableHelper.dispose(createBus);
    createBus = EventBus.get()
        .listen(TriggerCreateEvent.class)
        .subscribeOn(getSubscribeScheduler())
        .observeOn(getObserveScheduler())
        .subscribe(triggerCreateEvent -> createPowerTrigger(triggerCreateEvent.entry(), callback),
            throwable -> Timber.e(throwable, "onError create bus"));

    deleteBus = DisposableHelper.dispose(deleteBus);
    createBus = EventBus.get()
        .listen(TriggerDeleteEvent.class)
        .subscribeOn(getSubscribeScheduler())
        .observeOn(getObserveScheduler())
        .subscribe(triggerDeleteEvent -> deleteTrigger(triggerDeleteEvent.percent(), callback),
            throwable -> Timber.e(throwable, "onError create bus"));
  }

  @SuppressWarnings("WeakerAccess") void createPowerTrigger(@NonNull PowerTriggerEntry entry,
      @NonNull TriggerCreateCallback callback) {
    Timber.d("Create new power trigger");
    createDisposable = DisposableHelper.dispose(createDisposable);
    createDisposable = interactor.put(entry)
        .subscribeOn(getSubscribeScheduler())
        .observeOn(getObserveScheduler())
        .subscribe(callback::onNewTriggerAdded, throwable -> {
          Timber.e(throwable, "onError");
          if (throwable instanceof SQLiteConstraintException) {
            Timber.e("Error inserting into DB");
            callback.onNewTriggerInsertError();
          } else {
            Timber.e("Issue creating trigger");
            callback.onNewTriggerCreateError();
          }
        });
  }

  @SuppressWarnings("WeakerAccess") void deleteTrigger(int percent,
      @NonNull TriggerDeleteCallback callback) {
    deleteDisposable = DisposableHelper.dispose(deleteDisposable);
    deleteDisposable = interactor.delete(percent)
        .subscribeOn(getSubscribeScheduler())
        .observeOn(getObserveScheduler())
        .subscribe(callback::onTriggerDeleted, throwable -> Timber.e(throwable, "onError"));
  }

  /**
   * public
   */
  void loadTriggerView(@NonNull TriggerLoadCallback callback, boolean forceRefresh) {
    viewDisposable = DisposableHelper.dispose(viewDisposable);
    viewDisposable = interactor.queryAll(forceRefresh)
        .subscribeOn(getSubscribeScheduler())
        .observeOn(getObserveScheduler())
        .doAfterTerminate(callback::onTriggerLoadFinished)
        .subscribe(callback::onTriggerLoaded, throwable -> Timber.e(throwable, "onError"));
  }

  interface TriggerLoadCallback {

    void onTriggerLoaded(@NonNull PowerTriggerEntry entry);

    void onTriggerLoadFinished();
  }

  interface BusCallback extends TriggerDeleteCallback, TriggerCreateCallback {

  }

  interface TriggerDeleteCallback {

    void onTriggerDeleted(int position);
  }

  interface TriggerCreateCallback {

    void onNewTriggerAdded(@NonNull PowerTriggerEntry entry);

    void onNewTriggerCreateError();

    void onNewTriggerInsertError();
  }
}

@@ -2856,36 +2856,36 @@
 deleteBus);%0A    
-crea
+dele
 teBus = EventBus

Fix leak