import time
import random

from members.models import Member, Request
from infoscreen.models import ExternalImageInfoItem, Rotation, InfoInstance
from webapp.models import Tag, Feed
from misc.namegenerator import generate_names

from django.core.management.base import BaseCommand, CommandError


class Command(BaseCommand):

    def handle(self, *args, **options):

        print('THIS SCRIPT WILL GENERATE DUMMY VALUES TO DATABASE '
              'AND SHOULD __NEVER__ BE RUN ON PRODUCTION. '
              'IF YOU ARE ON PRODUCTION ABORT (ctrl-c) IMMEDIATELY!!!! '
              'CONTINUING IN 10 SECONDS')

        time.sleep(10)

        DOMAINS = ["example.coms", 'ggmail.om', "notmail.dom"]  # intentionally wrong
        PLACES = ["Helsinki", "Espoo", "Korso", "Kerava", "Kouvostoliitto"]
        MEMBER_COUNT = 30
        MEMBER_REQUEST_COUNT = 3

        names = generate_names(MEMBER_COUNT)
        for i in range(MEMBER_COUNT):
            first, last = names[i]
            mail = "{}.{}@{}".format(first.lower(), last.lower(), random.choice(DOMAINS))

            por = random.choice(PLACES)
            ayy = random.randint(0, 1)
            jas = random.randint(0, 1)
            Member.objects.create(first_name=first,
                                  last_name=last,
                                  email=mail,
                                  POR=por,
                                  AYY=ayy,
                                  jas=jas)

        i_item = ExternalImageInfoItem.objects.create(
            name="Heavy",
            url="https://i.imgur.com/XXSSqDG.gif"
        )

        rot = Rotation.objects.create(name="Demo")
        inst = InfoInstance.objects.create(
            rotation=rot,
            item=i_item,
            duration=20.0
        )

        names = generate_names(MEMBER_COUNT)
        for i in range(MEMBER_COUNT):
            first, last = names[i]
            mail = "{}.{}@{}".format(first.lower(), last.lower(), random.choice(DOMAINS))

            por = random.choice(PLACES)
            ayy = random.randint(0, 1)
            jas = random.randint(0, 1)
            Member.objects.create(first_name=first,
                                  last_name=last,
                                  email=mail,
                                  POR=por,
                                  AYY=ayy,
                                  jas=jas)

        TAGS = ["Party", "International", "Freshmen", "Culture"]
        TAG_COUNT = 2

        for i in range(TAG_COUNT):
            slug = TAGS[i] + str(random.randint(0, 10))
            name = slug + str(random.randint(0, 10))
            print(slug, name)
            Tag.objects.create(slug=slug,
                               name=name,
                               icon="http://testiurl.com/kuva.jpg")

        FEED_COUNT = 3

        for i in range(FEED_COUNT):
            title = "ds" + str(random.randint(0, 15))
            description = "dsg" + str(random.randint(0, 20))
            content = "fdfd"
            Feed.objects.create(title=title,
                                visible=True,
                                description=description,
                                content=content)
            tag1 = Tag.objects.get(id=1)
            Feed.objects.get(title=title).tags.add(tag1)
            tag2 = Tag.objects.get(id=1)
            Feed.objects.get(title=title).tags.add(tag2)
