package com.hikapro.backpack.model;

import android.os.Message;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.hikapro.backpack.App;
import com.hikapro.backpack.R;
import com.hikapro.backpack.model.dao.Command;
import com.hikapro.backpack.model.dao.DAO;
import com.hikapro.backpack.model.dao.Event;
import com.hikapro.backpack.model.entities.Set;
import com.hikapro.backpack.presenter.Presenter;


/**
 * Created by tariel on 20/04/16.
 */
public class SetModel implements Model.Set {

    private List<com.hikapro.backpack.model.entities.Set> cache;
    private Presenter.SetList   presenter;
    private DAO dao;
    private int lastVisiblePosition;


    public SetModel() {
        this.cache = new ArrayList<>();
        this.dao   = DAO.getInstance();
    }

    public int getLastVisiblePosition() {
        return lastVisiblePosition < 0 ? 0 : lastVisiblePosition;
    }

    public void setLastVisiblePosition(int lastVisiblePosition) {
        this.lastVisiblePosition = lastVisiblePosition;
    }

    @Override
    public com.hikapro.backpack.model.entities.Set getSetByPosition(int position) {
        return cache.get(position);
    }
    @Override
    public com.hikapro.backpack.model.entities.Set findSet(int id) {
        Set ret = null;
        for (Set s : cache) {
            if (s.getId() == id) {
                ret = s;
                break;
            }
        }
        return ret;
    }

    @Override
    public int getSetsCount() {
        return cache.size();
    }

    @Override
    public void onDestroy(boolean isConfigurationChanging) {
        if ( !isConfigurationChanging ) {
            presenter = null;
        }
        dao.unregisterObserver(this);
    }
    @Override
    public void notifyDataSetChanged() {
        presenter.notifyDataSetChanged();
    }

    @Override
    public void executeQuery() {
        dao.registerObserver(this);
        if (presenter != null)
            presenter.startProgress();
        Message command = Message.obtain();
        command.what = Command.SYNC;
        dao.executeCommand(command);
        command = Message.obtain();
        command.what = Command.SET_GET_ALL;
        dao.executeCommand(command);
    }

    @Override
    public void onEvent(Message event) {
        switch (event.what) {
            case Event.SYNC_NO_CONNECTION :
                if (presenter != null)
                    presenter.stopProgress();
                Toast.makeText(App.getAppContext(), R.string.no_connection, Toast.LENGTH_SHORT).show();
                Message command = Message.obtain();
                command.what = Command.SYNC_READ_FROM_FILE;
                dao.executeCommand(command);
                command = Message.obtain();
                command.what = Command.SET_GET_ALL;
                dao.executeCommand(command);
                break;
            case Event.SYNC_FAILED :
                if (presenter != null)
                    presenter.stopProgress();
                Toast.makeText(App.getAppContext(), "SYNC FAILED", Toast.LENGTH_SHORT).show();
                break;
            case Event.SET_LOAD_ERROR :
                if (presenter != null)
                    presenter.stopProgress();
                break;
            case Event.SET_ITEMS_LOAD_ERROR :
                break;
            case Event.SET_REORDER_ERROR :
                break;
            case Event.SET_LOAD_COMPLETED :
                if (presenter != null)
                    presenter.stopProgress();
                cache = (List<Set>) event.obj;
                notifyDataSetChanged();
                break;
            case Event.SET_ITEMS_LOAD_COMPLETED :
                break;
            case Event.SET_REORDER_COMPLETED :
                break;
        }
    }

    @Override
    public void setsReorderNotify() {
        for (int i = 0; i < cache.size(); ++i) {
            cache.get(i).setLineNumber(i);
        }
        Message command = Message.obtain();
        command.what = Command.SET_REORDER;
        command.obj = cache;
        dao.executeCommand(command);
    }

    @Override
    public void setPresenter(Presenter.SetList presenter) {
        this.presenter = presenter;
    }

    @Override
    public Presenter.SetList getPresenter() {
        return presenter;
    }

    @Override
    public List<com.hikapro.backpack.model.entities.Set> getSets()
    {
        return cache;
    }
}
