package com.hikapro.backpack.model;

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

import java.util.Collections;
import java.util.Hashtable;
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.Event;
import com.hikapro.backpack.model.entities.Category;
import com.hikapro.backpack.model.entities.Item;
import com.hikapro.backpack.presenter.adapters.helper.AppRater;

/**
 * Created by tariel on 12/05/16.
 */
public class PackedModel extends ItemModel {

    public PackedModel() {
        super();
    }

    @Override
    public void executeQuery() {
        Message command;
        dao.registerObserver(this);
        if (categoriesCache == null || categoriesCache.isEmpty()) {
            command = Message.obtain();
            command.what = Command.ITEM_GET_CATEGORIES;
            dao.executeCommand(command);
        }
        command = Message.obtain();
        command.what = Command.SET_GET_PACKED_ITEMS;
        command.arg1 = currentSet;
        dao.executeCommand(command);

        command = Message.obtain();
        command.what = Command.SET_GET_STAT;
        command.arg1 = presenter.getCurrentSet().getId();
        dao.executeCommand(command);
    }

    @Override
    public void filter(String query) {

        if (query.isEmpty()) {
            Message command = Message.obtain();
            command.what = Command.SET_GET_PACKED_ITEMS;
            command.arg1 = presenter.getCurrentSet().getId();
            dao.executeCommand(command);
        } else {
            Message command = Message.obtain();
            command.what = Command.SET_GET_ITEMS_LIKE_PACKED;
            command.arg1 = presenter.getCurrentSet().getId();
            command.obj = query;
            dao.executeCommand(command);
        }
    }

    @Override
    public void onEvent(Message event) {

        switch (event.what) {

            case Event.SET_PACKED_LOAD_ERROR :
                break;
            case Event.SET_UNPACK_ERROR :
                break;
            case Event.SET_GET_STAT_ERROR:
                break;
            case Event.ITEM_CATEGORY_LOAD_ERROR :
                break;
            case Event.ITEM_PACK_ERROR :
                break;
            case Event.ITEM_UNPACK_ERROR :
                break;
            case Event.ITEM_PENDING_REMOVAL_ERROR:
                break;
            case Event.ITEM_REMOVAL_CANCEL_ERROR:
                break;
            case Event.SET_ITEMS_LIKE_PACKED_ERROR:
                if (!itemsCache.isEmpty()) {
                    itemsCache.clear();
                }
                notifyDataSetChanged();
                break;
            case Event.SET_PACKED_LOAD_COMPLETED :
                itemsCache = (List<Item>) event.obj;
                notifyDataSetChanged();
                break;
            case Event.SET_ITEMS_LIKE_PACKED_COMPLETED:
                itemsCache = (List<Item>) event.obj;
                notifyDataSetChanged();
                break;
            case Event.SET_GET_STAT_COMPLETED:
                currentSetActiveItemsQty = event.arg1;
                packedQty = event.arg2;
                if (presenter != null)
                    presenter.notifyItemPackStatusChanged();
                break;
            case Event.ITEM_FROM_SET_DELETED:
                executeQuery();
                break;
            case Event.ITEM_PENDING_REMOVAL_COMPLETED:// TODO DEL
                executeQuery();
                break;
            case Event.ITEM_REMOVAL_CANCELED:
                executeQuery();
                break;
            case Event.ITEM_READ_COMPLETED:
                itemsCache.add((Item) event.obj);
                Collections.sort(itemsCache);
                notifyDataSetChanged();
                break;
            case Event.SET_UNPACK_COMPLETED :
                executeQuery();
                break;
            case Event.ITEM_CATEGORY_LOAD_COMPLETED :
                categoriesCache = (Hashtable<Integer, Category>)event.obj;
                break;
            case Event.ITEM_PACKED :
                executeQuery();
                break;
            case Event.ITEM_UNPACKED :
                executeQuery();
                break;
            case Event.SET_RESTORE_COMPLETED:
                executeQuery();
                Toast.makeText(App.getAppContext(), R.string.restore_completed, Toast.LENGTH_SHORT).show();
                break;
            case Event.NEED_TO_RATE_NOW:
                if (presenter != null)
                    presenter.showRateDialog();
                break;
        }
    }

    @Override
    public void unpackItem(com.hikapro.backpack.model.entities.Item item) {
        if (item != null) {
            Message command = Message.obtain();
            command.what = Command.ITEM_UNPACK;
            command.arg1 = currentSet;
            command.arg2 = item.getId();
            dao.executeCommand(command);
            itemsCache.remove(item); // TODO check nn
        }
    }

    @Override
    public void packItem(com.hikapro.backpack.model.entities.Item item) {
        if (item != null) {
            Message command = Message.obtain();
            command.what = Command.ITEM_REMOVAL_CANCEL;
            command.arg1 = currentSet;
            command.arg2 = item.getId();
            dao.executeCommand(command);
            itemsCache.remove(item);
        }
    }
}
