from django.contrib.sites.models import Site
from django.core.management import call_command

from .. import models
from . import (
    import_data,
)
from .resources import (
    MedianGroupResource,
    RatingCountGroupResource,
)
from municipal_finance.tests.helpers import BaseSeleniumTestCase


fixtures = {
    "geography": {
        "geo_level": "municipality",
        "geo_code": "BUF",
        "name": "Buffalo City",
        "long_name": "Buffalo City, Eastern Cape",
        "square_kms": 2751.69154949282,
        "parent_level": "province",
        "parent_code": "EC",
        "province_name": "Eastern Cape",
        "province_code": "EC",
        "category": "A",
        "miif_category": "A",
        "population": 781026,
        "postal_address_1": "P O BOX 134",
        "postal_address_2": "EAST LONDON",
        "postal_address_3": "5200",
        "street_address_1": "Trust Bank Centre",
        "street_address_2": "C/O Oxford & North Street",
        "street_address_3": "East London",
        "street_address_4": "5200",
        "phone_number": "043 705 2000",
        "fax_number": "043 743 8568",
        "url": "http://www.buffalocity.gov.za",
    }
}

class ScorecardTest(BaseSeleniumTestCase):
    fixtures = ["seeddata", "compiled_profile"]

    def setUp(self):
        super(ScorecardTest, self).setUp()

        self.geography = models.Geography.objects.create(
            **fixtures["geography"])
        import_data(
            MedianGroupResource,
            "views/median_group.csv",
        )
        import_data(
            RatingCountGroupResource,
            "views/rating_count_group.csv",
        )
        Site.objects.filter(id=2).update(domain='municipalmoney.org.za', name='Scorecard')

    def test_scorecard_formula(self):
        selenium = self.selenium
        selenium.get("%s%s" % (self.live_server_url, "/profiles/municipality-BUF-buffalo-city/"))

        self.wait_until_text_in("#cash-balance", "Cash Balance")
        selenium.find_elements_by_css_selector(".expand-block")[3].click()

        self.wait_until_text_in(".indicator-metric__value", "R13 000")
        self.wait_until_text_in(".indicator-calculation__formula-text", "Cash available at year end")
        self.wait_until_text_in(".is--post-2019-20", "[undefined] item code 4200, Audited Actual")
        self.wait_until_text_in(".is--pre-2019-20", "[Cash Flow] item code 4200, Audited Actual")

        element = selenium.find_element_by_css_selector('.is--post-2019-20 a').get_attribute("href")
        assert element == "http://portal:8002/table/cflow_v2/?municipalities=BUF&year=2019&items=4200&amountType=AUDA"
        element = selenium.find_element_by_css_selector('.is--pre-2019-20 a').get_attribute("href")
        assert element == "http://portal:8002/table/cflow/?municipalities=BUF&year=2019&items=4200&amountType=AUDA"

        is_displayed = selenium.find_element_by_css_selector('.is--pre-2019-20').is_displayed()
        assert is_displayed == True
        #call_command('loaddata', 'compiled_profile')
        #selenium = self.selenium
        #selenium.get("%s%s" % (self.live_server_url, "/profiles/municipality-BUF-buffalo-city/"))
        #is_displayed = selenium.find_element_by_css_selector('.is--pre-2019-20').is_displayed()
        #assert is_displayed == False
@@ -96,1090 +96,68 @@
 rom 
-.. import models%0Afrom . import (%0A    import_data,%0A)%0Afrom .resources import (%0A    MedianGroupResource,%0A    RatingCountGroupResource,%0A)%0Afrom municipal_finance.tests.helpers import BaseSeleniumTestCase%0A%0A%0Afixtures = %7B%0A    %22geography%22: %7B%0A        %22geo_level%22: %22municipality%22,%0A        %22geo_code%22: %22BUF%22,%0A        %22name%22: %22Buffalo City%22,%0A        %22long_name%22: %22Buffalo City, Eastern Cape%22,%0A        %22square_kms%22: 2751.69154949282,%0A        %22parent_level%22: %22province%22,%0A        %22parent_code%22: %22EC%22,%0A        %22province_name%22: %22Eastern Cape%22,%0A        %22province_code%22: %22EC%22,%0A        %22category%22: %22A%22,%0A        %22miif_category%22: %22A%22,%0A        %22population%22: 781026,%0A        %22postal_address_1%22: %22P O BOX 134%22,%0A        %22postal_address_2%22: %22EAST LONDON%22,%0A        %22postal_address_3%22: %225200%22,%0A        %22street_address_1%22: %22Trust Bank Centre%22,%0A        %22street_address_2%22: %22C/O Oxford & North Street%22,%0A        %22street_address_3%22: %22East London%22,%0A        %22street_address_4%22: %225200%22,%0A        %22phone_number%22: %22043 705 2000%22,%0A        %22fax_number%22: %22043 743 8568%22,%0A        %22url%22: %22http://www.buffalocity.gov.za%22,%0A    %7D%0A%7D
+municipal_finance.tests.helpers import BaseSeleniumTestCase%0A
 %0A%0Acl
@@ -224,16 +224,29 @@
 eddata%22,
+ %22demo-data%22,
  %22compil
@@ -326,319 +326,8 @@
 Up()
-%0A%0A        self.geography = models.Geography.objects.create(%0A            **fixtures%5B%22geography%22%5D)%0A        import_data(%0A            MedianGroupResource,%0A            %22views/median_group.csv%22,%0A        )%0A        import_data(%0A            RatingCountGroupResource,%0A            %22views/rating_count_group.csv%22,%0A        )
 %0A   

Fix CI