using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModelData.BusinessModel.RDF
{
    public static class Queries
    {
        #region Prefixes

        private static readonly Dictionary<string, string> Prefixes = new Dictionary<string, string>
        {
            ["rdf"] = "<http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
            ["rdfs"] = "<http://www.w3.org/2000/01/rdf-schema#>",
            ["owl"] = "<http://www.w3.org/2002/07/owl#>",
            ["xsd"] = "<http://www.w3.org/2001/XMLSchema#>",
            ["geo"] = "<http://www.w3.org/2003/01/geo/wgs84_pos#>",
            ["geonames"] = "<http://www.geonames.org/ontology#>",
            ["time"] = "<https://www.w3.org/2006/time#>",
            ["map"] = "<http://jena.denhome.keenetic.link/map#>",
            ["foaf"] = "<http://xmlns.com/foaf/0.1/>"
        };               

        #endregion

        #region Queries

        public static string AllSettlementsInInterval(string from, string to)
        {
            return GetAllPrefixes() +
@"
SELECT DISTINCT (?settlement as ?url) ?title ?lat ?long ?settlementType ?typeName ?region ?regionName (?maxc as ?maxPop)
WHERE
  {
    ?settlement a map:Settlement;
              geonames:name ?title;
              geo:lat ?lat; geo:long ?long.
    OPTIONAL {?settlement time:hasBeginning ?wasFound. ?wasFound time:year ?wasFoundYear}
  	FILTER (!bound(?wasFound) || ?wasFoundYear >= " + '"' + from + '"' + @"^^xsd:gYear )
    {
        SELECT ?settlement (MAX(?populationNotNULL) AS ?maxc)
        WHERE 
    	{ 
          ?editedSettlement a map:EditedSettlement; time:before ?settlement; time:hasBeginning ?date.
      	  ?date time:year ?year.
          FILTER  (?year >= " + '"' + from + '"' + @"^^xsd:gYear  && ?year <= " + '"' + to + '"' + @"^^xsd:gYear )
          OPTIONAL {?editedSettlement geonames:population ?population.}
		  OPTIONAL {?editedSettlement map:settlementType ?settlementType. ?settlementType geonames:name ?typeName}
		  
		  ## заполни переменную ?populationNotNULL так: если ?population НЕ задан (т.е. NULL), тогда 0, иначе (т.е. задан) ?population
		  BIND (IF (!BOUND(?population), " + "\"0\"" + @"^^xsd:integer, ?population) as ?populationNotNULL)
        }
        Group by ?settlement
  	}
	{
        SELECT ?settlement ?settlementType ?typeName ?region ?regionName
        WHERE 
    	{ 
          ?editedSettlement a map:EditedSettlement; time:before ?settlement; time:hasBeginning ?date.
      	  ?date time:year ?year.
          FILTER  (?year >= " + '"' + from + '"' + @"^^xsd:gYear  && ?year <= " + '"' + to + '"' + @"^^xsd:gYear )
		  OPTIONAL {?editedSettlement map:settlementType ?settlementType. ?settlementType geonames:name ?typeName}
      	  OPTIONAL {?editedSettlement rdfs:partOf ?region. ?region geonames:name ?regionName}
        }
  	}
  }
";
        }

        public static string InfoAboutSettlement(string URL)
        {
            string url_ = GetObjFromURL(URL);

            return GetAllPrefixes() +
@"SELECT DISTINCT ?title ?lat ?long ?wasFoundYear ?legend ?founder ?person ?foundersRegion ?editedSettlement ?source ?year ?settlementType ?typeName ?region ?regionName ?editedName ?alternateName ?population ?populationFemales ?populationMales
WHERE
{
	{ 
		SELECT * WHERE
		{
			" + url_ + @" geonames:name ?title;
					  geo:lat ?lat; geo:long ?long.

			OPTIONAL {" + url_ + @" time:hasBeginning ?wasFound. ?wasFound time:year ?wasFoundYear}
			OPTIONAL {" + url_ + @" map:legend ?legend.}
			OPTIONAL 
			{
				" + url_ + @" map:founder ?founder. 
				OPTIONAL {?founder foaf:Person ?person}
				OPTIONAL {?founder map:foundersRegion ?foundersRegion}
			}
		}
	}	
	UNION
	{
		SELECT * WHERE 
		{ 
			?editedSettlement a map:EditedSettlement; 
							time:before " + url_ + @"; 
							map:source ?source; 
							time:hasBeginning ?date.
			?date time:year ?year.
			OPTIONAL {?editedSettlement map:settlementType ?settlementType. ?settlementType geonames:name ?typeName}
			OPTIONAL {?editedSettlement rdfs:partOf ?region. ?region geonames:name ?regionName}
            OPTIONAL {?editedSettlement geonames:name ?editedName.}
			OPTIONAL {?editedSettlement geonames:alternateName ?alternateName.}
			OPTIONAL {?editedSettlement geonames:population ?population.}
			OPTIONAL {?editedSettlement map:populationFemalesCount ?populationFemales.}
			OPTIONAL {?editedSettlement map:populationMalesCount ?populationMales.}
			OPTIONAL {?editedSettlement map:settlementType ?settlementType. ?settlementType geonames:name ?typeName}
		}
	}
}";
        }

        public static string AllSettlementsName(string subName)
        {
            return GetPrefix("map") + GetPrefix("geonames") +
@"
select distinct ?name
where
{
  ?settlement a map:Settlement; geonames:name ?name.
  FILTER regex(?name, " + '"' + subName + "\", \"i\"" + @")
}
";
        }

        public static string Years()
        {
            return GetAllPrefixes() +
@"
SELECT ?min ?max
WHERE
  {
    {
        SELECT (MIN(?year) AS ?minEditedYear) (MAX(?year) AS ?maxEditedYear)
        WHERE 
    	{ 
          ?editedSettlement a map:EditedSettlement; time:hasBeginning ?date.
      	  ?date time:year ?year.  	  
        }        
  	}
	{
        SELECT (MIN(?minIndSetYear) AS ?minSetYear) (MAX(?maxIndSetYear) AS ?maxSetYear)
        WHERE 
    	{ 
          ?settlement a map:Settlement.
    	  OPTIONAL {?settlement time:hasBeginning ?wasFound. ?wasFound time:year ?wasFoundYear} 
     
      	## костыль
      ## если поля не существовали, MIN возвращал 1 вместо NULL, поэтому нарочно кастую к невозможным значениям, чтобы потом сравнить с существующими (см. другие два BIND) 
		  BIND (IF (!BOUND(?wasFound), " + "\"3000\"" + @"^^xsd:gYear, ?wasFoundYear) as ?minIndSetYear)
		  BIND (IF (!BOUND(?wasFound), " + "\"-3000\"" + @"^^xsd:gYear, ?wasFoundYear) as ?maxIndSetYear)
        }
	}
  ## выбрать наименьшую упоминаемую дату среди *наименьшей даты основания* и *наименьшей даты изменения*
    BIND (IF (?minSetYear < ?minEditedYear, ?minSetYear, ?minEditedYear) as ?min)
	BIND (IF (?maxSetYear > ?maxEditedYear, ?maxSetYear, ?maxEditedYear) as ?max)
  }
";
        }

        #endregion

        #region Tools

        private static string GetObjFromURL(string URL)
        {
            string[] input = URL.Split('#');
            string url_ = "";
            foreach (var item in Prefixes)
                if (item.Value.Contains(input[0]))
                {
                    url_ = item.Key + ":" + input[1];
                    break;
                }
            return (url_ != "") ? url_ : URL;
        }

        private static string GetPrefix(string key)
        {
            return "PREFIX " + key + ": " + Prefixes[key] + " ";
        }

        private static string GetAllPrefixes()
        {
            string t = "";
            foreach (var item in Prefixes)
            {
                t += "PREFIX " + item.Key + ": " + Prefixes[item.Key] + " ";
            }
            return t;
        }

        #endregion
    }

}

/*
 insert data
{
  map:TatishchevskijRegion a map:Region; geonames:name "Татищевский район".
}
     
     
     */

/*
FILTER regex(?name, "^ali", "i")
^ дает указаение, что искомое стоит в начале слова == "Alice"
FILTER regex(str(?mbox), "@work.example")
@ дает указаение, что искомое стоит в конце слова == <mailto:alice@work.example>
i - параметр, указывающий что регистр поиска не важен
ПОИСК (по regex) здесь: https://www.w3.org/TR/rdf-sparql-query/
 */

