package kz.arta.synergy.workflow.mobile.android.files.offline;

import android.content.Context;

import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.List;

import kz.arta.synergy.workflow.mobile.android.common.models.Person;
import kz.arta.synergy.workflow.mobile.android.common.offline.get.LogsTable;
import kz.arta.synergy.workflow.mobile.android.common.requests.RestClient;
import kz.arta.synergy.workflow.mobile.android.common.requests.SendGetRequest;
import kz.arta.synergy.workflow.mobile.android.common.utils.DateUtils;
import kz.arta.synergy.workflow.mobile.android.common.utils.MyApplication;
import kz.arta.synergy.workflow.mobile.android.files.models.Node;

/**
 * Created by neox on 29.08.16.
 * Синхронизация файлов
 */
public class FilesSync {

    private SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DEFAULT_DATE_JOURNAL_SYNC);
    private LogsTable offlineLogs;
    private double random;

    private Person person;
    private SendGetRequest sendGetRequest;

    private FilesFiltersTable filesFiltersTable;
    private FilesListTable filesListTable;

    public FilesSync(Person person, Context context, double random) {

        this.person = person;
        this.random = random;

        sendGetRequest = new SendGetRequest(person);
        offlineLogs = new LogsTable(context);

        filesFiltersTable = new FilesFiltersTable(context);
        filesFiltersTable.removeAll();

        filesListTable = new FilesListTable(context);
        filesListTable.removeAll();
    }

    /**
     * Метод вызвыается в первую очередь
     */
    public void allSync() {
        getFilters(person.getHost() + "rest/api/storage/list?getOnlyFolders=true&locale=" + MyApplication.getLang());
    }

    private void getFilters(String url) {
        String result = sendGetRequest.requestGET(url);
        filesFiltersTable.add(url, result);
        List<Node> folders = null;
        try {
            folders = RestClient.getGson().fromJson(result, new TypeToken<List<Node>>() {}.getType());
        } catch (Exception exc) {
            // handling error ....
        }
        if (folders != null) {
            for (Node folder : folders) {
                getList(person.getHost() + "rest/api/storage/list?parentID=" + folder.getIdentifier() + "&locale=" + MyApplication.getLang());
                if (folder.isNotEmpty()) {
                    getFilters(person.getHost() + "rest/api/storage/list?parentID=" + folder.getIdentifier() + "&getOnlyFolders=true&locale=" + MyApplication.getLang());
                }
            }
        }
    }

    private void getList(String url) {
        String result = sendGetRequest.requestGET(url);
        Node[] nodes = RestClient.getGson().fromJson(result, Node[].class);
        for (Node node : nodes) {
            offlineLogs.add("1", "Элемент хранилища " + "\"" + node.getName() + "\"" + " успешно синхронизирован",
                    sdf.format(System.currentTimeMillis()), random + "");
        }
        filesListTable.add(url, result);
    }
}
