/*-
 * #%L
 * This file is part of "Apromore Community".
 *
 * Copyright (C) 2015 - 2017 Queensland University of Technology.
 * %%
 * Copyright (C) 2018 - 2020 The University of Melbourne.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

package com.apql.Apql.tree;

import com.apql.Apql.controller.QueryController;
import org.apromore.helper.Version;
import org.apromore.model.ProcessSummaryType;
import org.apromore.model.VersionSummaryType;
import org.apromore.service.pql.ExternalId;

import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by corno on 2/08/2014.
 */
public class DraggableNodeProcess extends DraggableNodeTree {

//    private List<VersionSummaryType> versions;
    private String latestVersion;
//    private String originalLanguage;
    private String latestBranch;
//    private String domain;
//    private String ranking;
    private ProcessSummaryType pst;
    private QueryController queryController=QueryController.getQueryController();

    public DraggableNodeProcess(ProcessSummaryType pst, String path){
        super(pst.getId().toString(),pst.getName(),path);
//        this.versions=pst.getVersionSummaries();
        String lastUpdate=null;
        VersionSummaryType version=null;
        for(VersionSummaryType vst : pst.getVersionSummaries()) {
            if (lastUpdate == null || vst.getLastUpdate().compareTo(lastUpdate) > 0) {
                lastUpdate = vst.getLastUpdate();
                version = vst;
            }
            queryController.addLocation(new ExternalId(pst.getId(), vst.getName(), new Version(vst.getVersionNumber())),this);
        }
        this.pst=pst;
        this.latestVersion=version.getVersionNumber();
        this.latestBranch=version.getName();

//        this.originalLanguage=pst.getOriginalNativeType();
//        this.domain=pst.getDomain();
//        this.ranking=pst.getRanking();
    }

    public ProcessSummaryType getValue(){
        return pst;
    }

    public VersionSummaryType getVersionSummaryType(String idNet) {
        try {
            ExternalId externalId = new ExternalId(idNet);
            for(VersionSummaryType vst : pst.getVersionSummaries()){
                if(pst.getId() != externalId.getProcessId())
                    throw new IllegalArgumentException("Wrong ID net");
                if(vst.getVersionNumber().equals(externalId.getVersion().toString()) && vst.getName().equals(externalId.getBranch())){
                    return vst;
                }
            }
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<VersionSummaryType> getVersions() {
        return pst.getVersionSummaries();
    }

    public void setVersions(List<VersionSummaryType> versions) {
        this.pst.getVersionSummaries().clear();
        this.pst.getVersionSummaries().addAll(versions);
    }

    public String getLatestVersion() {
        return latestVersion;
    }

    public void setVersionBranch(String latestVersion) {
        this.latestVersion = latestVersion;
    }

    public String getLatestBranch() {
        return latestBranch;
    }

    public void setLatestBranch(String latestBranch) {
        this.latestBranch = latestBranch;
    }

    public String getOriginalLanguage() {
        return pst.getOriginalNativeType();
    }

    public void setOriginalLanguage(String originalLanguage) {
        this.pst.setOriginalNativeType(originalLanguage);
    }

    public String getDomain() {
        return pst.getDomain();
    }

    public void setDomain(String domain) {
        this.pst.setDomain(domain);
    }

    public String getRanking() {
        return pst.getRanking();
    }

    public void setRanking(String ranking) {
        this.pst.setRanking(ranking);
    }

    // Object methods

    @Override public boolean equals(Object ob){
        if(ob == null || !(ob instanceof DraggableNodeProcess)){
            return false;
        }
        if(ob == this)
            return true;
        DraggableNodeProcess dnp=(DraggableNodeProcess)ob;
        return dnp.getId().equals(getId()) && dnp.getLatestVersion().equals(latestVersion) && dnp.getLatestBranch().equals(latestBranch);
    }

    @Override public int hashCode(){
        return getId().hashCode()*latestVersion.hashCode()*latestBranch.hashCode();
    }

    @Override public String toString() {
        return "DNP(id=" + getId() + " branch=" + latestBranch + " version=" + latestVersion + ")";
    }
}
