package org.owntracks.android.ui.preferences.load;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.owntracks.android.data.repos.WaypointsRepo;
import org.owntracks.android.injection.qualifier.AppContext;
import org.owntracks.android.injection.scopes.PerActivity;
import org.owntracks.android.messages.MessageConfiguration;
import org.owntracks.android.support.Parser;
import org.owntracks.android.support.Preferences;
import org.owntracks.android.ui.base.viewmodel.BaseViewModel;

import java.io.IOException;

import javax.inject.Inject;

import timber.log.Timber;


@PerActivity
public class LoadViewModel extends BaseViewModel<LoadMvvm.View> implements LoadMvvm.ViewModel<LoadMvvm.View> {
    private final Preferences preferences;
    private final Parser parser;
    private final WaypointsRepo waypointsRepo;

    private MessageConfiguration configuration;

    @Inject
    public LoadViewModel(@AppContext Context context, Preferences preferences, Parser parser, WaypointsRepo waypointsRepo) {
        this.preferences = preferences;
        this.parser = parser;
        this.waypointsRepo = waypointsRepo;
    }

    public void attachView(@Nullable Bundle savedInstanceState, @NonNull LoadMvvm.View view) {
        super.attachView(savedInstanceState, view);
    }


    public String setConfiguration(String json) throws IOException, Parser.EncryptionException {
        Timber.v("%s", json);

        this.configuration = (MessageConfiguration) parser.fromJson(json.getBytes());

        Timber.v("hasWaypoints: %s / #%s", configuration.hasWaypoints(), configuration.getWaypoints().size());
        return parser.toJsonPlainPretty(this.configuration);

    }



    public void saveConfiguration() {
        preferences.importFromMessage(configuration);

        if(configuration.hasWaypoints()) {
            waypointsRepo.importFromMessage(configuration.getWaypoints());
        }

        getView().showFinishDialog();
    }

    @Override
    public boolean hasConfiguration() {
        return this.configuration != null;
    }
}

@@ -1489,25 +1489,24 @@
 %25s%22, json);%0A
-%0A
         this
@@ -1582,120 +1582,8 @@
 ());
-%0A%0A        Timber.v(%22hasWaypoints: %25s / #%25s%22, configuration.hasWaypoints(), configuration.getWaypoints().size());
 %0A   
@@ -1644,18 +1644,15 @@
 n);%0A
-%0A
     %7D%0A%0A
-%0A%0A
     

Fix for #818 - NPE when import config waypoints was null