from django.db import models
from reqman.apps.account.models import User

import jsonfield

from pygments.formatters.html import HtmlFormatter
from pygments import highlight

from reqman.apps.reqtool.rest_api.services.parse_ontologies import *


SUFFIX_CHOICES = (("",""), ('S1', 'S1: <numerical-affirmative> j <closed-interval> [per <time-unit>]'),
                   ('S2', 'S2: after/before flow'),
                   ('S3', 'S3: [every/for a period of/within/for at least] <number> <time-unit> [from flow]'),
                   ('S4', 'S4: at the beginning/at the end'),
                   ('S5', 'S5: at even intervals'),
                   ('S6', 'S6: sequentially')
                 )
NUMERICAL_AFFIRMATIVE_CHOICES = (("MORE THAN", "MORE THAN"), ("LESS THAN", "LESS THAN"), ("EXACTLY","EXACTLY"), 
                                ("AT LEAST", "AT LEAST"), ("AT MOST", "AT MOST"))
#NUMBER_UNITS_CHOICES = (("",""),("m/s","m/s"), ("m/s^2","m/s^2"), ("m/s^3","m/s^3"), ("rad","rad"), ("rad/s","rad/s"), ("Hz","Hz"), ("METERS","METERS"), ("KILOMETERS","KILOMETERS"), ("VOLT","VOLT"))

#TIME_UNITS_CHOICES = (("SECONDS","SECONDS"), ("MINUTES","MINUTES"), ("MILLISECONDS","MILLISECONDS"), ("CYCLES","CYCLES"))
#FLOW_CHOICES = get_instances("SAO#Flow")
# FLOW returns ()
S2_CHOICES = (("BEFORE","BEFORE"), ("AFTER","AFTER"))
S3_CHOICES = (("EVERY","EVERY"), ("FOR A PERIOD OF","FOR A PERIOD OF"), ("WITHIN", "WITHIN"), ("FOR AT LEAST", "FOR AT LEAST"))
S4_CHOICES = (("AT THE BEGINNING","AT THE BEGINNING"), ("AT THE END","AT THE END"))
S5_6_CHOICES = (("AT EVEN INTERVALS","AT EVEN INTERVALS"), ("SEQUENTIALLY","SEQUENTIALLY"))
S5_6_CHOICES = (("AT EVEN INTERVALS","AT EVEN INTERVALS"), ("SEQUENTIALLY","SEQUENTIALLY"))

#S_CHOICES = NUMERICAL_AFFIRMATIVE_CHOICES + S2_CHOICES + S3_CHOICES + S4_CHOICES + S5_6_CHOICES

'''g = Graph()
g.load('../../Ontologies/2022_AOCS.ttl', format="turtle")
S_CHOICES = get_instances_g("RBO#Preposition",g) + get_instances_g("RBO#NumericalAffirmative",g)
FLOW_CHOICES=(("SECONDS","SECONDS"), ("MINUTES","MINUTES"), ("MILLISECONDS","MILLISECONDS"), ("CYCLES","CYCLES"))
NUMBER_UNITS_CHOICES=get_instances_g("LO#NumberUnit",g)
SYSTEM_CHOICES = get_instances_g("SAO#System",g)
print(SYSTEM_CHOICES)'''


def create_suffix(instance):
    '''Create the suffix Model for the Boilerplate'''
    try :
        suffix = Suffix.objects.get(boilerplate_of_suffix = instance, suffix_owner = instance.owner)
        if instance.has_suffix:
            return
        suffix = Suffix.objects.get(boilerplate_of_suffix = instance, suffix_owner = instance.owner)
        suffix.delete()
    except Suffix.DoesNotExist:
        if instance.has_suffix:
            suffix = Suffix(boilerplate_of_suffix = instance, suffix_owner = instance.owner)
            suffix.save()



class Suffix(models.Model):
    boilerplate_of_suffix = models.ForeignKey('reqtool.Boilerplate', related_name='suffix_boilerplate', on_delete=models.CASCADE)
    suffix_owner = models.ForeignKey(User, related_name='suffix_owner', on_delete=models.CASCADE)
    #prefix templates
    #s_choices = models.CharField(choices=S_CHOICES, max_length=100, blank=True)
    s_choices = models.CharField(max_length=100, blank=True)
    #if S1
    numerical = models.CharField(blank=True, null=True, max_length=10)
    #mumerical_units = models.CharField(choices=NUMBER_UNITS_CHOICES, max_length=100, blank=True)
    mumerical_units = models.CharField(max_length=100, blank=True)
    #time_units = models.CharField(choices=TIME_UNITS_CHOICES, max_length=100, blank=True)
    #if S2/S3
    #flow = models.CharField(choices=FLOW_CHOICES, max_length=100, blank=True)
    flow = models.CharField(max_length=100, blank=True)
    #find suffix    
    suffix_choices = models.CharField(choices=SUFFIX_CHOICES, max_length=100, blank=True)
    #custom suffix
    custom_suffix = jsonfield.JSONField()


    class Meta:
        ordering = ['boilerplate_of_suffix']