import sys
import os
import pytest
import json

sys.path.insert(0, os.path.realpath(os.path.dirname(__file__))+'/../../')

#from settings import config
from cuchulu.libraries.db.webmodel import PhangoField, WebModel
from cuchulu.libraries.db import corefields
from cuchulu.libraries.db.extrafields.arrayfield import ArrayField
from cuchulu.libraries.db.extrafields.colorfield import ColorField
from cuchulu.libraries.db.extrafields.datefield import DateField
from cuchulu.libraries.db.extrafields.datetimefield import DateTimeField
from cuchulu.libraries.db.extrafields.dictfield import DictField
from cuchulu.libraries.db.extrafields.emailfield import EmailField
from cuchulu.libraries.db.extrafields.i18nfield import I18nField
from cuchulu.libraries import datetime

class ExampleModel(WebModel):
    
    def __init__(self, connection=None):

        super().__init__(connection)

        # I can change other fields here, how the name.

        self.register(corefields.CharField('title'))
        self.register(corefields.CharField('content'))


def test_test_phangofield():
    
    field=PhangoField('default', 255)

    assert field.check(' "<trial>" ')=='&quot;&lt;trial&gt;&quot;'

def test_test_integerfield():
    
    field=corefields.IntegerField('int')
    
    assert field.check("25")=='25'
    
    assert field.check("25j")=='0'
    
    field.check("25j")
    
    assert field.error==True

def test_test_floadfield():
    
    field=corefields.FloatField('int')
    
    assert field.check('1,5')=='1.5'
    
    assert field.check('1.5456')=='1.5456'
    
    assert field.check('1.5456tet')=='0'

def test_test_htmlfield():
    
    field=corefields.HTMLField('html')
    
    assert field.check('<p>"trial"</p><script></script>')=='&lt;p&gt;"trial"&lt;/p&gt;&lt;script&gt;&lt;/script&gt;'

    field.escape=True
    
    assert field.check('<p>"trial"</p><script></script>')=='&lt;p&gt;&quot;trial&quot;&lt;/p&gt;&lt;script&gt;&lt;/script&gt;'

    field.trusted_tags=['p']
    
    assert field.check('<p>"trial"</p><script></script>')=='<p>&quot;trial&quot;</p>&lt;script&gt;&lt;/script&gt;'

    #field.
def test_test_foreignkeyfield():
    
    field=corefields.ForeignKeyField('foreign', ExampleModel())
    
    assert field.check('dsd')==None

def test_test_booleanfield():
    
    field=corefields.BooleanField('bool')
    
    assert field.check('25')=='0'
    
    assert field.error==True
    
    
def test_test_arrayfield():
    
    field=ArrayField('array', corefields.IntegerField('item'))
    
    assert field.check([1, 2, 3])=='["1", "2", "3"]'
    
    assert field.check([1, 2, '3t'])=='["1", "2", "0"]'
    
    assert field.error==True

def test_test_colorfield():
    
    field=ColorField('color')
    
    assert field.check('#ff12f5')==0xff12f5
    
    assert field.check('#ff12f54')==0
    
    assert field.get_hex_color(0xaf42f5)=='#af42f5'
    
def test_test_datefield():
    
    datetime.timezone='Europe/Madrid'
        
    datetime.set_timezone()
    
    field=DateField('date')
    
    field.utc=False
    
    assert field.check('20201234121011')==''
    
    assert field.check('20201230121011')=='20201230121011'
    
def test_test_datetimefield():
    
    datetime.timezone='Europe/Madrid'
        
    datetime.set_timezone()
    
    field=DateTimeField('date')
    
    field.utc=False
    
    assert field.check('20201230121011')=='2020-12-30 12:10:11'
    
    assert field.check('20201290121011')=='0000-00-00 00:00:00'

def test_test_dictfield():
    
    field=DictField('dict', corefields.IntegerField('item'))
    
    assert field.check({'come': '5'})=='{"come": "5"}'
    
    assert field.check({'come': '5t'})=='{"come": "0"}'

    assert field.error==True

def test_test_emailfield():
    
    field=EmailField('email')
    
    assert field.check('example@example.com')=='example@example.com'
    
    assert field.check('example-example.com')==''
    
def test_test_i18nfield():
    
    field=I18nField('i18nfield')
    
    assert field.check(json.dumps({'en-US': "Hello", 'es-ES': "Hola"}))=='{"en-US": "Hello", "es-ES": "Hola"}'
    
    assert field.check(json.dumps({'en-US': "Hello"}))=='{"en-US": "Hello", "es-ES": ""}'
    
    pass
    
