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

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

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


#find_subclass_domain_range()
#MAIN SYNTAX

import pprint
#pprint.pprint(get_main_sytax())

MAIN_CHOICES = ( ("",""),   ("M1", "M1"), 
                            ("M2", "M2"), 
                            ("M3", "M3"),
                            ("M4", "M4"),
                            ("M5", "M5"),
                            ("M6", "M6"),
                            ("M7", "M7"),
                            ("M8", "M8"),
                            ("M9", "M9"),
                            ("M10", "M10"),
                            ("M11", "M11"),
                            ("M12", "M12"),
                            ("M13", "M13"),
                            ("M14", "M14"),
                            ("M15", "M15"),
                            ("M16", "M16"),
                        )


#to be updated with domains from DSO
#SYSTEM
#SYSTEM_CHOICES = get_instances("SAO#System") #+get_i_instances("SAO#System")
#SYSTEM_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#System")
#SYSTEM_SUBCLASSES_COMMENTS = get_dmo_classes_of_classes_and_comment("SAO#System")
#SYSTEM_INSTANCES_COMMENTS = get_dmo_instance_and_comment("SAO#System")
#FUNTION
#FUNCTION_CHOICES = get_instances("SAO#Function")
#FUNCTION_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#Function")
#FUNCTION_SUBCLASSES_COMMENTS = get_dmo_classes_of_classes_and_comment("SAO#Function")
#FUNCTION_INSTANCES_COMMENTS = get_dmo_instance_and_comment("SAO#Function")
#
SHALL_CHOICES = ( ("shall", "shall"), ("shall not", "shall not"))
#QUANTIFIER_CHOICES = ( ("None","None"), ("All", "All"), ("Only", "Only"),
#                                ("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"), ("Number", "Number"))
##g = Graph()
#g.load('../../Ontologies/2022_AOCS.ttl', format="turtle")
#PREFIX_SYNTAX = get_prefix_syntax(g)
#S_CHOICES = get_instances_g("RBO#Preposition",g) + get_instances_g("RBO#NumericalAffirmative",g)
#print(S_CHOICES)

#ITEM
#ITEM_CHOICES = get_instances("SAO#Item")
#ITEM_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#Item")
#ITEM_SUBCLASSES_COMMENTS = get_dmo_classes_of_classes_and_comment("SAO#Item")
#ITEM_INSTANCES_COMMENTS = get_dmo_instance_and_comment("SAO#Item")
#
#Interface
#INTERFACE_CHOICES = get_instances("SAO#Interface")
#INTERFACE_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#Interface")
#INTERFACE_SUBCLASSES_COMMENTS = get_dmo_classes_of_classes_and_comment("SAO#Interface")
#INTERFACE_INSTANCES_COMMENTS = get_dmo_instance_and_comment("SAO#Interface")
#
#Connection
#CONNECTION_CHOICES = get_instances("SAO#Connection")
#CONNECTION_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#Connection")
#CONNECTION_SUBCLASSES_COMMENTS = get_dmo_classes_of_classes_and_comment("SAO#Connection")
#CONNECTION_INSTANCES_COMMENTS = get_dmo_instance_and_comment("SAO#Connection")
#
#Flow
#FLOW_CHOICES = get_instances("SAO#Flow")
#FLOW_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#Flow")
#FLOW_SUBCLASSES_COMMENTS = get_dmo_classes_of_classes_and_comment("SAO#Flow")
#FLOW_INSTANCES_COMMENTS = get_dmo_instance_and_comment("SAO#Flow")
#
#State
#STATE_CHOICES = get_instances("SAO#State")
#STATE_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#State")
#STATE_SUBCLASSES_COMMENTS = get_dmo_classes_of_classes_and_comment("SAO#State")
#STATE_INSTANCES_COMMENTS = get_dmo_instance_and_comment("SAO#State")
#State Choices
#STATE_SET_CHOICES = get_instances("SAO#StateSet")
#STATECHOICES_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#StateSet")
#
#State Values
#STATE_VALUE_CHOICES =get_instances("SAO#StateValue")
#STATEVALUES_CLASSES_COMMENTS = get_dmo_classes_and_comment("SAO#StateValue")
#

#VERB_CHOICES = get_verb_instances("LO#Verb")
#VERB_CHOICES = get_verb_file_and_instances("LO#Verb")

#test to add M
CLASS_CHOICES = (("SYSTEM","SYSTEM"), ("FUNCTION","FUNCTION"), ("SHALL","SHALL/SHALL NOT"),
                ("QUANTIFIER","QUANTIFIER"), ("NUMBER_UNITS","NUMBER_UNITS"), ("ITEM","ITEM"),
                ("STATE_VALUE","STATE_VALUE"), ("STATE","STATE"), ("STATE_SET","STATE_SET"),
                ("FLOW","FLOW"), ("INTERFACE","INTERFACE"), ("CONNECTION","CONNECTION"), ("VERB","VERB") )

def create_main(instance):
    '''Create the Main Model for the Boilerplate'''
    try :
        main = Main.objects.get(boilerplate_of_main = instance, main_owner = instance.owner)
        if instance.has_main:
            return
        main = Main.objects.get(boilerplate_of_main = instance, main_owner = instance.owner)
        main.delete()
    except Main.DoesNotExist:
        if instance.has_main:
            main = Main(boilerplate_of_main = instance, main_owner = instance.owner)
            main.save()


class Main(models.Model):
    boilerplate_of_main = models.ForeignKey('reqtool.Boilerplate', related_name='main_boilerplate', on_delete=models.CASCADE)
    main_owner = models.ForeignKey(User, related_name='main_owner', on_delete=models.CASCADE)
    #Subject
    sys_fun_inter = models.CharField(max_length=1000)
    #Between Subject - Verb
    state_item_before_verb = models.CharField(max_length=1000, blank=True)
    statevalue_before_verb = models.CharField(max_length=1000, blank=True)
    #Verb
    shall = models.CharField(max_length=1000)
    verb = models.CharField(max_length=1000)
    #Between Verb - Object
    quantifier = models.CharField(max_length=1000, blank=True)
    numerical = models.CharField(blank=True, null=True, max_length=1000) 
    mumerical_units = models.CharField(max_length=1000, blank=True)
    #Οbject
    flow_function_interface_item_system_state_stateset = models.CharField( max_length=1000)
    #last definitions
    statevalue_system_connection_stateset = models.CharField(max_length=1000, blank=True)
    #ID [M1-M16]
    main_choices = models.CharField(max_length=100, blank=True)

    class Meta:
        ordering = ['boilerplate_of_main']

