from StageHubAPI import db
from StageHubAPI.model import Project, Group, Status, Stage, Step, Episode, Shot, Vendor, Studio, Task, TaskStatus, Account
from StageHubAPI.model import ProjectGroup, ProjectStage, ProjectStep
import sys
import random
import hashlib

db.drop_all()
db.create_all()

s1 = Studio(name='CGCG', code_name='cgtp')

v1 = Vendor(name='Lucas Animation', code_name='LAL')
v2 = Vendor(name='Dream Work Animation', code_name='DWA')

db.session.add(s1)
db.session.add(v1)
db.session.add(v2)
db.session.flush()

print(v1.id)

# db.session.flush()
p1 = Project(name='Cookies', code_name='cookies',
             vendor_id=v1.id, description='Star Wars Season II', studio_id=s1.id)
p2 = Project(name='JUR', code_name='jur', vendor_id=v2.id,
             description='Jurassic Park Season II', studio_id=s1.id)
p3 = Project(name='NDR', code_name='ndr', vendor_id=v2.id,
             description='How to train your dragon Season IV', studio_id=s1.id)

db.session.add(p1)
db.session.add(p2)
db.session.add(p3)

db.session.commit()

projects = ['Cookies', 'JUR', 'NDR']

eps = {'Cookies': ['EP01', 'EP02', 'EP03'],
       'JUR': ['JUR_001', 'JUR_002', 'JUR_003'],
       'NDR': ['NDR_101', 'NDR_102', 'NDR_103']}

act = ['']

for p in projects:
    p_id = projects.index(p) + 1
    for e in eps.get(p):
        episode = Episode(name=e, project_id=p_id)
        db.session.add(episode)

db.session.flush()

status = []
st1 = Status(name='ready', description='Ready to Start')
st2 = Status(name='wip', description='Working in Progress')
st3 = Status(name='finished', description='Finished')
st4 = Status(name='cbb', description='Could be Better')

status.append(st1)
status.append(st2)
status.append(st3)
status.append(st4)
for s in status:
    db.session.add(s)

db.session.flush()

task_status = []
rds = TaskStatus(name='ready', description='Ready to Start')
wip = TaskStatus(name='wip', description='Working in Progress')
pnd = TaskStatus(name='pending', description='Pending')
rtk = TaskStatus(name='retake', description='Retake')
apr = TaskStatus(name='approve', description='Approve')
cbb = TaskStatus(name='cbb', description='Could be Better')

task_status.append(rds)
task_status.append(wip)
task_status.append(pnd)
task_status.append(rtk)
task_status.append(apr)
task_status.append(cbb)

for ts in task_status:
    db.session.add(ts)

# Stages
stages = []
mdl = Stage(name='modeling', code_name='MDL', type='assets')
stages.append(mdl)
sfc = Stage(name='surfacing', code_name='SFC', type='assets')
stages.append(sfc)
rig = Stage(name='rigging', code_name='RIG', type='assets')
stages.append(rig)
dmp = Stage(name='matte_painting', code_name='DMP', type='assets')
stages.append(dmp)
hair = Stage(name='hair', code_name='HAIR', type='assets')
stages.append(hair)
xgen = Stage(name='xgen', code_name='XGEN', type='assets')
stages.append(xgen)
ani = Stage(name='animation', code_name='ANI', type='shots')
stages.append(ani)
lay = Stage(name='layout', code_name='LAY', type='shots')
stages.append(lay)
sim = Stage(name='simulation', code_name='SIM', type='shots')
stages.append(sim)
fx = Stage(name='fx', code_name='FX', type='shots')
stages.append(fx)
drs = Stage(name='dressing', code_name='DRS', type='shots')
stages.append(drs)
lgt = Stage(name='lighting', code_name='LGT', type='shots')
stages.append(lgt)
cmp = Stage(name='compositing', code_name='COMP', type='shots')
stages.append(cmp)

for stg in stages:
    db.session.add(stg)
db.session.flush()

stp_blk = Step(name='Blocking', code_name='BLK')
db.session.add(stp_blk)
stp_pri = Step(name='Primary', code_name='PRI')
db.session.add(stp_pri)
stp_sec = Step(name='Secondary', code_name='SEC')
db.session.add(stp_sec)
stp_ani = Step(name='Animation', code_name='ANI')
db.session.add(stp_ani)


# Project Stage
p1_ani = ProjectStage(project_id=p1.id, stage_id=ani.id)
db.session.add(p1_ani)
p2_ani = ProjectStage(project_id=p2.id, stage_id=ani.id)
db.session.add(p2_ani)

db.session.flush()

# Steps
p1_ani_blk = ProjectStep(project_stage_id=p1_ani.id, step_id=stp_blk.id)
db.session.add(p1_ani_blk)
p1_ani_ani = ProjectStep(project_stage_id=p1_ani.id, step_id=stp_ani.id)
db.session.add(p1_ani_ani)

p2_ani_blk = ProjectStep(project_stage_id=p2_ani.id, step_id=stp_blk.id)
db.session.add(p2_ani_blk)
p2_ani_pri = ProjectStep(project_stage_id=p2_ani.id, step_id=stp_pri.id)
db.session.add(p2_ani_pri)
p2_ani_sec = ProjectStep(project_stage_id=p2_ani.id, step_id=stp_sec.id)
db.session.add(p2_ani_sec)
p2_ani_ani = ProjectStep(project_stage_id=p2_ani.id, step_id=stp_ani.id)
db.session.add(p2_ani_ani)

db.session.flush()

a1 = Account(login='indigo', first_name='唐', last_name='慕霖', mail='indigo@cgcg.com.tw', type='inhouse', studio_id=s1.id,
             password=hashlib.md5('test@1234'.encode('utf8')).hexdigest())
a2 = Account(login='admin', first_name='admin', last_name='', mail='admin@cgcg.com.tw', type='inhouse', studio_id=s1.id,
             password=hashlib.md5('admin@1234'.encode('utf8')).hexdigest(), admin=True)

db.session.add(a1)
db.session.add(a2)

# Stages
groups = []
mdl = Group(name='modeling', type='department')
groups.append(mdl)
sfc = Group(name='surfacing', type='department')
groups.append(sfc)
rig = Group(name='rigging', type='department')
groups.append(rig)
dmp = Group(name='matte_painting', type='department')
groups.append(dmp)
ani = Group(name='animation', type='department')
groups.append(ani)
lay = Group(name='layout', type='department')
groups.append(lay)
sim = Group(name='simulation', type='department')
groups.append(sim)
fx = Group(name='fx', type='department')
groups.append(fx)
lgt = Group(name='lighting', type='department')
groups.append(lgt)
cmp = Group(name='compositing', type='department')
groups.append(cmp)

for grp in groups:
    db.session.add(grp)
db.session.flush()

for p in projects:
    p_id = projects.index(p) + 1
    for e in eps.get(p):
        for i in range(100):
            shot_name = '{}_{}_{:03d}'.format(p, e, i)
            shot = Shot(name=shot_name, project_id=p_id, type='main', status=st1.name)
            db.session.add(shot)
            db.session.flush()
            for stg in stages:
                ts_id = random.randint(0, len(task_status)-1)
                # print(ts_id, len(task_status))
                task = Task(source_id=shot.id, name=stg.name, stage=stg.name, type='shot',
                            status=task_status[ts_id].name, author_id=a1.id)
                db.session.add(task)
    for grp in groups:
        db.session.add(ProjectGroup(project_id=p_id, group_id=grp.id))

db.session.commit()
