package com.zelory.kace.adressbook.presenter;

import com.zelory.kace.adressbook.data.DataManager;
import com.zelory.kace.adressbook.data.model.AddressBook;
import rx.Observable;
import rx.schedulers.Schedulers;

import javax.swing.*;
import java.io.File;

public class AddressBookPresenter {
    private View view;

    public AddressBookPresenter(View view) {
        this.view = view;
    }

    public void loadAddressBook(File file) {
        view.showLoading();
        DataManager.getInstance()
                .loadAddressBook(file)
                .subscribeOn(Schedulers.io())
                .subscribe(addressBook -> SwingUtilities.invokeLater(() -> {
                    view.showAddressBook(addressBook);
                    view.dismissLoading();
                }), throwable -> SwingUtilities.invokeLater(() -> {
                    view.onError(throwable);
                    view.dismissLoading();
                }));
    }

    public void saveAddressBook(AddressBook addressBook, String path) {
        view.showLoading();
        DataManager.getInstance()
                .save(addressBook, path)
                .subscribeOn(Schedulers.io())
                .subscribe(savedAddressBook -> SwingUtilities.invokeLater(() -> {
                    view.showAddressBook(savedAddressBook);
                    view.dismissLoading();
                }), throwable -> SwingUtilities.invokeLater(() -> {
                    view.onError(throwable);
                    view.dismissLoading();
                }));
    }

    public void sortPersonsByName(AddressBook addressBook) {
        view.showLoading();
        Observable.from(addressBook.getPersons())
                .toSortedList((person1, person2) -> person1.getName().compareTo(person2.getName()))
                .doOnNext(addressBook::setPersons)
                .subscribeOn(Schedulers.io())
                .subscribe(savedAddressBook -> SwingUtilities.invokeLater(() -> {
                    view.showAddressBook(addressBook);
                    view.dismissLoading();
                }), throwable -> SwingUtilities.invokeLater(() -> {
                    view.onError(throwable);
                    view.dismissLoading();
                }));
    }

    public void sortPersonsByZip(AddressBook addressBook) {
        view.showLoading();
        Observable.from(addressBook.getPersons())
                .toSortedList((person1, person2) -> person1.getZip().compareTo(person2.getZip()))
                .doOnNext(addressBook::setPersons)
                .subscribeOn(Schedulers.io())
                .subscribe(savedAddressBook -> SwingUtilities.invokeLater(() -> {
                    view.showAddressBook(addressBook);
                    view.dismissLoading();
                }), throwable -> SwingUtilities.invokeLater(() -> {
                    view.onError(throwable);
                    view.dismissLoading();
                }));
    }

    public interface View {

        void showAddressBook(AddressBook addressBook);

        void showLoading();

        void dismissLoading();

        void onError(Throwable throwable);
    }
}

@@ -815,32 +815,81 @@
 keLater(() -%3E %7B%0A
+                    throwable.printStackTrace();%0A
                 
@@ -889,34 +889,36 @@
            view.
-on
+show
 Error(throwable)
@@ -908,32 +908,45 @@
 wError(throwable
+.getMessage()
 );%0A             
@@ -1469,32 +1469,81 @@
 keLater(() -%3E %7B%0A
+                    throwable.printStackTrace();%0A
                 
@@ -1543,34 +1543,36 @@
            view.
-on
+show
 Error(throwable)
@@ -1562,32 +1562,45 @@
 wError(throwable
+.getMessage()
 );%0A             
@@ -2233,32 +2233,81 @@
 keLater(() -%3E %7B%0A
+                    throwable.printStackTrace();%0A
                 
@@ -2307,34 +2307,36 @@
            view.
-on
+show
 Error(throwable)
@@ -2326,32 +2326,45 @@
 wError(throwable
+.getMessage()
 );%0A             
@@ -2994,32 +2994,81 @@
 keLater(() -%3E %7B%0A
+                    throwable.printStackTrace();%0A
                 
@@ -3076,18 +3076,20 @@
    view.
-on
+show
 Error(th
@@ -3091,24 +3091,37 @@
 or(throwable
+.getMessage()
 );%0A         
@@ -3345,34 +3345,36 @@
 oid 
-on
+show
 Error(
-Throwable throwabl
+String errorMessag
 e);%0A

Refactor onError to showError