package com.hikapro.backpack.model;

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

import com.hikapro.backpack.App;
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.Item;
import com.hikapro.backpack.presenter.Presenter;

import java.util.Collections;

/**
 * Created by tariel on 23/04/16.
 */
public class DetailModel implements Model.Detail {

    private Presenter.ItemDetail    presenter;
    private Item                    item;
    private DAO                     dao;
    private Bitmap                  pic;
    private int                     currentSet;

    public DetailModel() {
        this.dao = DAO.getInstance();
    }

    // detail -->

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Item getCurrentItem() {
        return item;
    }

    @Override
    public void setCurrentItem(Item item) {
        this.item = item;
    }

    @Override
    public Bitmap getPicture() {
        return pic;
    }
    // detail <--

    // events -->

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

    // events <--

    // process -->
    @Override
    public void executeQuery() {
        dao.registerObserver(this);

        Message command = Message.obtain();
        command.what = Command.ITEM_READ;
        command.arg1 = currentSet;
        command.arg2 = item.getId();
        dao.executeCommand(command);

        command = Message.obtain();
        command.what = Command.ITEM_GET_IMAGE;
        command.arg1 = item.getId();
        dao.executeCommand(command);
    }

    @Override
    public void pendingRemove(Item item) {
        if (item != null) {
            Message command = Message.obtain();
            command.what = Command.ITEM_PENDING_REMOVAL;
            command.arg1 = currentSet;
            command.arg2 = item.getId();
            dao.executeCommand(command);
        }
    }

    @Override
    public void pendingRemoveCancel(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);
        }
    }

    @Override
    public void packItem(Item item) {
        if (item != null) {
            Message command = Message.obtain();
            command.what = Command.ITEM_PACK;
            command.arg1 = currentSet;
            command.arg2 = item.getId();
            dao.executeCommand(command);
        }
    }

    @Override
    public void unpackItem(Item item) {
        if (item != null) {
            Message command = Message.obtain();
            command.what = Command.ITEM_UNPACK;
            command.arg1 = currentSet;
            command.arg2 = item.getId();
            dao.executeCommand(command);
        }
    }

    // process <--


    @Override
    public void setPresenter(Presenter.ItemDetail presenter) {
        this.presenter  = presenter;
        this.currentSet = presenter.getSetId();
    }

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

    @Override
    public void onEvent(Message event) {

        switch (event.what) {

            case Event.ITEM_IMAGE_LOAD_ERROR:
                pic = null;
                if (presenter != null)
                    presenter.notifyPictureChanged();
                break;
            case Event.ITEM_READ_ERROR:
                Toast.makeText(App.getAppContext(), "Item read error", Toast.LENGTH_SHORT).show();
                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.ITEM_IMAGE_LOAD_COMPLETED:
                pic = (Bitmap) event.obj;
                if (presenter != null)
                    presenter.notifyPictureChanged();
                break;
            case Event.ITEM_PACKED:
                executeQuery();
                break;
            case Event.ITEM_UNPACKED:
                executeQuery();
            case Event.ITEM_PENDING_REMOVAL_COMPLETED:
                executeQuery();
            case Event.ITEM_REMOVAL_CANCELED:
                executeQuery();
                break;
            case Event.ITEM_READ_COMPLETED:
                item = (Item) event.obj;
                notifyDataSetChanged();
                break;
        }

    }
}
