function Cell (i,j){
        this.items = []
        this.row = i;
        this.col = j;


   this.isEmpty = function (){
        return this.items.length === 0;
    }

    this.add = function(item, init){
       if(typeof init === 'undefined'){
           init = false;
       }
        if (init){
            if (!this.isEmpty()) {
                return false
            }
        }
        if (typeof item !== "object" || item.name == null || this.contains(item)){
            return false;
        } else {
            this.items.push(item);
            return true;
        }
    }

    this.remove = function(item){
        let i = this.locationOf(item);
        if (i > -1) {
            this.items.splice(i, 1);
            return true
        } else {
            return false;
        }
    }

    this.locationOf = function(o){
        let index = -1;
        if (typeof o !="object"){
            if(typeof o !="string"){
                return index;
            }

            this.items.forEach(function(val, i){
                if (o === val.name) {
                    index = i
                }
                 });

        } else{
            this.items.forEach(function(val, i){
                if (o.name === val.name) {
                    index = i
                }
                 }); 
            
        }
        return index;
    }


    this. contains = function(item){
        return this.locationOf(item) > -1;
    }

}

function Item (name){
       this.name = name;
       this.cell = null;


    this.equals = function(item) {
        return this.name === item.name
    }

    this.moveup = function(grid) {
       return grid.update(this, this.cell.row -1,this.cell.col)
    }
     this.movedown = function(grid) {
        return grid.update(this, this.cell.row+1,this.cell.col)
    }
     this.moveleft = function(grid) {
       return grid.update(this, this.cell.row ,this.cell.col - 1)
    }
     this.moveright = function (grid) {
        return grid.update(this, this.cell.row ,this.cell.col + 1)
    }
    
}

// CREATES A MATRIX OF WIDTH AND HEIGHT SPECIFIED ;
function MatrixGrid(w, h) {
        this.matrix = []
        for (let i = 0; i < h; i++ ) {
            let row = []
            for (let j = 0; j < w; j++ ) {
                row.push(new Cell(i,j))
        }
        this.matrix.push(row)
    }

    // INSERTS CONTENT SPECIFIED BY THE ITEM FUNCTION ABOVE AT THE POSITION INDICATED ;
    this.insert = function(item, x,y, init){
            if(typeof init === 'undefined'){
                init = false;
            }
        let cell = this.matrix[x][y];
        let suc = cell.add(item, init);
        if (suc) item.cell = cell;
        return suc ;
    }

    // DELETES CONTENT SPECIFIED BY THE ITEM FUNCTION ABOVE AT THE POSITION INDICATED ;
    this.delete = function(item, x, y){
        let cell = this.matrix[x][y];
        return cell.remove(item);
    }

    this.update = function(item, i, j) {
        let row = this.matrix[i];
        if (row === null || row === undefined)
         return false;
        let newCell = row[j]
        if (newCell === null || newCell === undefined)
         return false;
        let old = item.cell;
        if (old.remove(item)) {
            item.cell = newCell
            newCell.add(item)
            return true
        }
        return false
    }

}

export {Item, MatrixGrid};
