/*
 * Copyright (c) 2014-2022, Draque Thompson, draquemail@gmail.com
 * All rights reserved.
 *
 * Licensed under: MIT Licence
 * See LICENSE.TXT included with this code to read the full license agreement.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package org.darisadesigns.polyglotlina.Nodes;

import org.darisadesigns.polyglotlina.ManagersCollections.FamilyManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.darisadesigns.polyglotlina.WebInterface;

/**
 *
 * @author draque
 */
public class FamNode extends DictNode {
    private final Set<FamNode> subNodes = new HashSet<>();
    private final Set<ConWord> words = new HashSet<>();
    private final FamNode parentNode;
    private String notes = "";
    private final FamilyManager manager;
    
    /**
     * sets notes
     * @param _notes new notes
     */
    public void setNotes(String _notes) {
        notes = _notes;
    }
    
    /**
     * Gets node's parent
     * @return FamNode representing node's parent. null if root
     */
    public FamNode getParentNode() {
        return parentNode;
    }
    
    /**
     * gets notes
     * @return notes of node
     */
    public String getNotes() {
        return notes;
    }
    
    /**
     * sets parent of node
     * @param _parent node's parent (null if root)
     * @param _manager a link to the parent manager
     */    
    public FamNode(FamNode _parent, FamilyManager _manager) {
        parentNode = _parent;
        manager = _manager;
    }
    
    /**
     * sets parent and value of node
     * @param _parent parent of note (null if root)
     * @param _value node's string value
     * @param _manager A link to the parent manager
     */
    public FamNode(FamNode _parent, String _value, FamilyManager _manager) {
        parentNode = _parent;
        this.setValue(_value);
        manager = _manager;
    }
    
    @Override
    public final void setValue(String _value) {
        super.setValue(_value);
    }
    
    /**
     * gets node's manager
     * @return Family Manager
     */
    public FamilyManager getManager() {
        return manager;
    }
    
    /**
     * NOT IMPLEMENTED IN FAMILYNODE
     * @param _node NOTHING
     */
    @Override
    public void setEqual(DictNode _node) throws ClassCastException {
        throw new ClassCastException("setEqual should never be called on FamNode instances.");
    }
        
    /**
     * adds word to family. ignores dupes
     * @param _word the word to add
     */
    public void addWord(ConWord _word) {
        if (!words.contains(_word)) {
            words.add(_word);
        }
    }
    
    /**
     * removes word from family
     * @param _word id of word to remove
     */
    public void removeWord(ConWord _word) {
        words.remove(_word);
    }
    
    /**
     * gets all words in immediate family
     * @return iterator of all words in immediate family
     */
    public ConWord[] getWords() {
        manager.removeDeadWords(this, words);

        List<ConWord> ret = new ArrayList<>(words);
        Collections.sort(ret);
        
        return ret.toArray(new ConWord[0]);
    }
    
    /**
     * returns all words within family and subfamilies
     * @return sorted list of ConWords
     */
    public ConWord[] getWordsIncludeSubs() {
        List<ConWord> ret = getWordsIncludeSubsInternal();
        Collections.sort(ret);
        
        return ret.toArray(new ConWord[0]);
    }
    
    /**
     * internally facing, recursive method for getting all words in this and subfamilies
     * @return list of (non duped) words in this and all subnodes
     */
    private List<ConWord> getWordsIncludeSubsInternal() {
        manager.removeDeadWords(this, words);
        
        List<ConWord> ret = new ArrayList<>(words);
        
        Iterator<FamNode> subIt = subNodes.iterator();
        
        while (subIt.hasNext()) {
            FamNode curNode = subIt.next();
            
            Iterator<ConWord> wordIt = curNode.getWordsIncludeSubsInternal()
                    .iterator();
            
            while (wordIt.hasNext()) {
                ConWord curWord = wordIt.next();
                
                // only add current word to return value if not already present
                if (!ret.contains(curWord)) {
                    ret.add(curWord);
                }
            }
        }
        
        return ret;
    }
    
    /**
     * gets all subnodes
     * @return alphabetically sorted iterator of all subnodes
     */
    public FamNode[] getNodes() {
        var ret = new ArrayList<FamNode>(subNodes);
        Collections.sort(ret);
        
        return ret.toArray(new FamNode[0]);
    }
    
    public void addNode(FamNode _node) {
        subNodes.add(_node);
    }
    
    /** 
     * removes self from parent, does nothing of root
     * @return false if root
     */
    public boolean removeFromParent() {
        if (parentNode == null) {
            return false;
        }
        
        parentNode.removeChild(this);
        
        return true;
    }
    
    /**
     * removes given child from this parent node
     * @param _child 
     */
    public void removeChild(FamNode _child) {
        subNodes.remove(_child);
    }
    
    @Override
    public boolean equals(Object comp) {
        boolean ret = false;
        
        if (this == comp) {
            ret = true;
        } else if (comp != null && getClass() == comp.getClass()) {
            FamNode c = (FamNode)comp;
            
            ret = value.trim().equals(c.value.trim());
            ret = ret && subNodes.equals(c.subNodes);
            ret = ret && words.equals(c.words);
            ret = ret && WebInterface.getTextFromHtml(notes).equals(WebInterface.getTextFromHtml(c.notes));
        }
        
        return ret;
    }

    @Override
    public int hashCode() {
        // node is mutable, so we cannot use hashing
        return 1;
    }
}
