﻿package clients;

import java.util.ArrayList;
import java.util.HashMap;

import fleXplorer.FacetedTaxonomy.FT;
import fleXplorer.Facets.Facet;
import fleXplorer.Facets.IndexType;
import fleXplorer.MaterializedFacetedTaxonomies.Counters;
import fleXplorer.MaterializedFacetedTaxonomies.MFT_MEM;
import fleXplorer.MaterializedFacetedTaxonomies.ObjectFacet;
import fleXplorer.Taxonomies.CheckRel;
import fleXplorer.Taxonomies.HasLists;
import fleXplorer.Taxonomies.Taxonomy;
import fleXplorer.Terms.Comparison;
import fleXplorer.Types.Document;
import fleXplorer.Types.FacetsType;
import fleXplorer.Types.StringType;
import fleXplorer.util.IndexesSetting.Documents;

public class ConsoleClient {

	private MFT_MEM materializedTaxonomy;

	public ConsoleClient(){
		materializedTaxonomy = new MFT_MEM("Mitos DB", Counters.TRUE, ObjectFacet.YES); // Creates a new materialized faceted taxonomy
		createFT(); //Creates the faceted taxonomy
	}

	private void createFT() {
		FT facetedTaxonomy = new FT();

		// Filetypes
		Taxonomy<StringType> filetypeTaxonomy = new Taxonomy<StringType>("Filetypes", CheckRel.FALSE, Comparison.NONCOMPARABLE, HasLists.FALSE, "String");
		// Domains
		Taxonomy<StringType> domainsTaxonomy = new Taxonomy<StringType>("Domains", CheckRel.FALSE, Comparison.NONCOMPARABLE, HasLists.TRUE, "String");
		// Encodings
		Taxonomy<StringType> encodingsTaxonomy = new Taxonomy<StringType>("Encodings", CheckRel.FALSE, Comparison.NONCOMPARABLE, HasLists.FALSE,"String");
			
		/*------ Creates each taxonomy -------*/
		createFiletypesTaxonomy(filetypeTaxonomy);
		createDomainsTaxonomy(domainsTaxonomy);
		createEncodingsTaxonomy(encodingsTaxonomy);

		// Add taxonomies to facets.
		Facet<StringType> facet1 = new Facet<StringType>("By filetype", filetypeTaxonomy, IndexType.COMPLEX);
		Facet<StringType> facet2 = new Facet<StringType>("By domain", domainsTaxonomy, IndexType.SIMPLE);
		Facet<StringType> facet3 = new Facet<StringType>("By encoding", encodingsTaxonomy, IndexType.SIMPLE);

		// Add facets to facetTaxonomy
		facetedTaxonomy.add(facet1);
		facetedTaxonomy.add(facet2);
		facetedTaxonomy.add(facet3);

		// Add facet taxonomy to materialized faceted taxonomy
		materializedTaxonomy.setFT(facetedTaxonomy);
	}

	private void createFiletypesTaxonomy(Taxonomy<StringType> filetypeTaxonomy) {
		/*------- This facet is flat, for this reason the developer just add each term to the terminology.  -------*/
		filetypeTaxonomy.setTerm(new StringType("pdf"));
		filetypeTaxonomy.setTerm(new StringType("doc"));
		filetypeTaxonomy.setTerm(new StringType("ppt"));
		filetypeTaxonomy.setTerm(new StringType("htm"));
		filetypeTaxonomy.setTerm(new StringType("html"));
		filetypeTaxonomy.setTerm(new StringType("none"));
		filetypeTaxonomy.setTerm(new StringType("pps"));
		filetypeTaxonomy.setTerm(new StringType("rtf"));
		filetypeTaxonomy.setTerm(new StringType("txt"));
	}

	private void createDomainsTaxonomy(Taxonomy<StringType> domainsTaxonomy) {
		/*------- This facet is hierarchically organized, for this reason the developer sets the relationships.  -------*/
		domainsTaxonomy.addHead(new StringType("gr"));
		domainsTaxonomy.setRelship(new StringType("gr"), new StringType("uoc gr"));
		domainsTaxonomy.setRelship(new StringType("gr"), new StringType("forth.gr"));
		domainsTaxonomy.setRelship(new StringType("uoc gr"), new StringType("csd.uoc.gr"));
		domainsTaxonomy.setRelship(new StringType("forth.gr"), new StringType("ics.forth.gr"));
	}

	private void createEncodingsTaxonomy(Taxonomy<StringType> encodingsTaxonomy) {
		/*------- This facet is flat, for this reason the developer just add each term to the terminology.  -------*/
		encodingsTaxonomy.setTerm(new StringType("iso-8859-1"));
		encodingsTaxonomy.setTerm(new StringType("iso8859-1"));
		encodingsTaxonomy.setTerm(new StringType("ISO-8859-1"));
		encodingsTaxonomy.setTerm(new StringType("iso-8859-7"));
		encodingsTaxonomy.setTerm(new StringType("ISO-8859-7"));
		encodingsTaxonomy.setTerm(new StringType("us-ascii"));
		encodingsTaxonomy.setTerm(new StringType("utf-8"));
		encodingsTaxonomy.setTerm(new StringType("UTF-8"));
		encodingsTaxonomy.setTerm(new StringType("windows-1252"));
		encodingsTaxonomy.setTerm(new StringType("windows-1253"));
	}
	
	public static void main(String[] args) {

		/*--------------- A new ConsoleClient Object --------------*/
		ConsoleClient cc = new ConsoleClient();
		
		ArrayList<Document> docs = new ArrayList<Document>();
		
		HashMap<String, FacetsType> st1 = new HashMap<String, FacetsType>();
		st1.put("By domain", new StringType("ics.forth.gr"));
		st1.put("By encoding", new StringType("iso-8859-1"));
		st1.put("By filetype", new StringType("doc"));
		Document doc1 = new Document(11,11,st1);
		docs.add(doc1);
		
		HashMap<String, FacetsType> st2 = new HashMap<String, FacetsType>();
		st2.put("By domain", new StringType("ics.forth.gr"));
		st2.put("By encoding", new StringType("iso-8859-1"));
		st2.put("By filetype", new StringType("txt"));
		Document doc2 = new Document(12,12,st2);
		docs.add(doc2);
		
		HashMap<String, FacetsType> st3 = new HashMap<String, FacetsType>();
		st3.put("By domain", new StringType("csd.uoc.gr"));
		st3.put("By encoding", new StringType("utf-8"));
		st3.put("By filetype", new StringType("doc"));
		Document doc3 = new Document(13,13,st3);
		docs.add(doc3);
		
		HashMap<String, FacetsType> st4 = new HashMap<String, FacetsType>();
		st4.put("By domain", new StringType("csd.uoc.gr"));
		st4.put("By encoding", new StringType("utf-8"));
		st4.put("By filetype", new StringType("doc"));
		Document doc4 = new Document(14,14,st4);
		docs.add(doc4);
		
		HashMap<String, FacetsType> st5 = new HashMap<String, FacetsType>();
		st5.put("By domain", new StringType("csd.uoc.gr"));
		st5.put("By encoding", new StringType("iso-8859-7"));
		st5.put("By filetype", new StringType("doc"));
		Document doc5 = new Document(15,15,st5);
		docs.add(doc5);
		
		HashMap<String, FacetsType> st6 = new HashMap<String, FacetsType>();
		st6.put("By domain", new StringType("ics.forth.gr"));
		st6.put("By encoding", new StringType("iso-8859-7"));
		st6.put("By filetype", new StringType("txt"));
		Document doc6 = new Document(16,16,st6);
		docs.add(doc6);
		
		HashMap<String, FacetsType> st7 = new HashMap<String, FacetsType>();
		st7.put("By domain", new StringType("ics.forth.gr"));
		st7.put("By encoding", new StringType("utf-8"));
		st7.put("By filetype", new StringType("html"));
		Document doc7 = new Document(17,17,st7);
		docs.add(doc7);
		
		/*----------------- Documents object is the collection --------------*/
		Documents dts = new Documents(cc.materializedTaxonomy, docs);
		dts.setIndexes(); // Creates the extensions of each term.
		
		
		cc.materializedTaxonomy.computeLegalIds(); // computes the ins that belong to the extension of the focus. At first the focus is the top element of each facet.
		cc.materializedTaxonomy.cumputeZoomInPoints(); //computes the zoom-in points
		HashMap<String, Taxonomy<?>> tmp = cc.materializedTaxonomy.getZoomInPoints(); // gets the zoom-in points
		cc.materializedTaxonomy.printFacetedTree(tmp); // prints the faceted trees. It prints all terms. For each term prints the value, the count information, the id and the objects that belong to the extension of the term.
		System.out.println(cc.materializedTaxonomy.getDescriptionOfFocus()); //prints the focus.
		
		System.out.println("---------------------------------------------------------");
		
		/*------  Set the focus with the id of the zoom-in point  ------*/ 
		cc.materializedTaxonomy.setFocusWithIds("By domain", "not 2");
		cc.materializedTaxonomy.computeLegalIds();
		cc.materializedTaxonomy.cumputeZoomInPoints();
		tmp = cc.materializedTaxonomy.getZoomInPoints();
		cc.materializedTaxonomy.printFacetedTree(tmp);
		
		System.out.println(cc.materializedTaxonomy.getDescriptionOfFocus());
		System.out.println("LegalIds: "+cc.materializedTaxonomy.getLegalIds());
		
		System.out.println("---------------------------------------------------------");
		
		/*------  Set the focus with a boolean expression  ------*/ 
		cc.materializedTaxonomy.setFocus("{By filetype : doc} AND {By domain : csd.uoc.gr} AND {By encoding : iso-8859-7}");
		cc.materializedTaxonomy.computeLegalIds();
		cc.materializedTaxonomy.cumputeZoomInPoints();
		tmp = cc.materializedTaxonomy.getZoomInPoints();
		cc.materializedTaxonomy.printFacetedTree(tmp);
		
		System.out.println(cc.materializedTaxonomy.getDescriptionOfFocus());
		System.out.println("LegalIds: "+cc.materializedTaxonomy.getLegalIds());

	}

}