from otree.api import Currency as c, currency_range
from ._builtin import Page, WaitPage
from .models import Constants
import random


class S0_Instructions(Page):
    pass


class S1_1InformedConsent(Page):
    pass


class S1_2InformedConsent(Page):
    form_model = 'player'
    form_fields = ['city', 'day', 'month', 'year', 'hour', 'minutes', 'ampm', 'name', 'signature', 'idcard', 'citycard']


class S2_1Transport(Page):
    form_model = 'player'
    form_fields = ['dircity1', 'dircity2', 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', 'dir6', 'dir7', 'dir8',
                   't3', 't4_1', 't4_2', 't5', 't6_1', 't6_2', 't7', 't8', 't9', 't10']


class S2_2Transport(Page):
    form_model = 'player'
    form_fields = ['t11_1', 't11_2', 't11_3', 't11_4', 't11_5', 't11_6', 't11_7', 't11_8', 't11_9',
                   't12_1', 't12_2', 't12_3', 't13_1', 't13_2', 't13_3', 't14_1', 't14_2', 't14_3',
                   't15_1', 't15_2', 't15_3', 't16_1', 't16_2', 't16_3', 't17_1', 't17_2', 't17_3',
                   't18_1', 't18_2', 't18_3', 't19_1', 't19_2', 't19_3']

    def is_displayed(self):
        return self.player.t10 == 1


class S3_UseOfTime(Page):
    form_model = 'player'
    form_fields = ['u1_1', 'u1_2', 'u1_3', 'u2_1', 'u2_2', 'u2_3', 'u3_1', 'u3_2', 'u3_3',
                   'u4_1', 'u4_2', 'u4_3', 'u5_1', 'u5_2', 'u5_3', 'u6_1', 'u6_2', 'u6_3',
                   'u7_1', 'u7_2', 'u7_3']


class S4_MorningQuestionnaire(Page):
    form_model = 'player'
    form_fields = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10', 'm11', 'm12', 'm13']


class S5_1Instructions(Page):
    def vars_for_template(self):
        randomX = self.player.randomX
        randomX_min = randomX - c(15)
        randomX_max = randomX + c(15)

        return dict(
            randomX=randomX, randomX_min=randomX_min, randomX_max=randomX_max
        )


class S5_2Game(Page):
    form_model = 'player'
    form_fields = ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11']

    def vars_for_template(self):
        randomX = self.player.randomX
        randomX_b1 = randomX - c(15)
        randomX_b2 = randomX - c(10)
        randomX_b3 = randomX - c(7.5)
        randomX_b4 = randomX - c(5)
        randomX_b5 = randomX - c(2.5)
        randomX_b6 = randomX
        randomX_b7 = randomX + c(2.5)
        randomX_b8 = randomX + c(5)
        randomX_b9 = randomX + c(7.5)
        randomX_b10 = randomX + c(10)
        randomX_b11 = randomX + c(15)

        return dict(
            randomX=randomX, randomX_b1=randomX_b1, randomX_b2=randomX_b2, randomX_b3=randomX_b3, randomX_b4=randomX_b4,
            randomX_b5=randomX_b5, randomX_b6=randomX_b6, randomX_b7=randomX_b7, randomX_b8=randomX_b8,
            randomX_b9=randomX_b9, randomX_b10=randomX_b10, randomX_b11=randomX_b11
        )


class S5_3Results(Page):

    def vars_for_template(self):
        randomX = self.player.randomX

        # Esta es la situación que escogerá el computador.
        random_s = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

        if random_s == 1:
            decision = self.player.s1
        elif random_s == 2:
            decision = self.player.s2
        elif random_s == 3:
            decision = self.player.s3
        elif random_s == 4:
            decision = self.player.s4
        elif random_s == 5:
            decision = self.player.s5
        elif random_s == 6:
            decision = self.player.s6
        elif random_s == 7:
            decision = self.player.s7
        elif random_s == 8:
            decision = self.player.s8
        elif random_s == 9:
            decision = self.player.s9
        elif random_s == 10:
            decision = self.player.s10
        else:
            decision = self.player.s11

        if decision == 1:
            pago = randomX
        else:
            if random_s == 1:
                pago = randomX - c(15)
            elif random_s == 2:
                pago = randomX - c(10)
            elif random_s == 3:
                pago = randomX - c(7.5)
            elif random_s == 4:
                pago = randomX - c(5)
            elif random_s == 5:
                pago = randomX - c(2.5)
            elif random_s == 6:
                pago = randomX
            elif random_s == 7:
                pago = randomX + c(2.5)
            elif random_s == 8:
                pago = randomX + c(5)
            elif random_s == 9:
                pago = randomX + c(7.5)
            elif random_s == 10:
                pago = randomX + c(10)
            else:
                pago = randomX + c(15)

        if decision == 1:
            h1 = '9:00 am-10:00am'
        else:
            h1 = '8:00 pm-9:00pm'

        self.player.random_s = random_s

        self.player.pagoS5 = pago

        return dict(randomX=randomX, random_s=random_s, decision=decision, pago=pago, h1=h1)


class S6_1Security(Page):
    form_model = 'player'
    form_fields = ['v1_1', 'v1_2', 'v1_3', 'v1_4', 'v1_5', 'v1_6', 'v1_7',
                   'v2_1', 'v2_2', 'v2_3', 'v2_4']


class S6_2Security(Page):
    pass


class S6_3Security(Page):
    pass


class S7_1Payment(Page):
    form_model = 'player'
    form_fields = ['namep', 'idcard1', 'address', 'cityp', 'telephone', 'dayp', 'monthp', 'yearp', 'value',
                   'signaturep', 'idcard2', 'typepayment']

    def vars_for_template(self):

        positiony = 0
        vector_labels = ['a.campos', 'a.gomezj', 'a.rojasn', 'a.rubio', 'a.yung', 'ac.puentes', 'ad.lugo', 'af.amaya',
                         'af.martinezv1',
                         'ag.alvarez10', 'ag.estupinan10', 'am.manrique', 'am.otavo10', 'am.riano', 'am.rinconr',
                         'am.tellez10', 'bd.gonzalezr1', 'bd.solorzano', 'c.morales10', 'ca.anaya', 'ca.azuero',
                         'ca.duran', 'ca.garcia12', 'ca.guerrero', 'ca.vegav', 'cd.cuervo', 'cd.espitiad',
                         'cd.forigua', 'cd.robles', 'cl.gonzalezg', 'd.nieto', 'd.torresf', 'da.bernal11',
                         'da.cifuentes', 'da.cruz128', 'da.monsalve10', 'da.sandoval10', 'dc.navarro', 'dd.blanco10',
                         'df.beltran12', 'df.osorio12', 'df.rangelq', 'df.sanchez18', 'dj.villamil', 'dp.cruz',
                         'e.cubillose', 'er.alvis1048', 'f.marroquin10', 'g.camargo10', 'g.chavez', 'ge.marquez',
                         'gv.valdivieso2309', 'h.zaraza10', 'i.buitron', 'i.oteros', 'i.quiroga', 'j.bonilla',
                         'j.dix', 'j.florezg', 'j.mejiao', 'j.ortiza', 'j.ramirez28', 'j.salazar10', 'ja.calderonh',
                         'ja.castrom', 'ja.gomezg', 'jc.cardenasb1', 'jc.gonzalezv', 'jc.matiz10', 'jd.rios13',
                         'jd.silvap', 'jd.vega1754', 'je.cardona', 'je.chaves', 'je.sanchezg', 'jf.lopeze',
                         'ji.quinones', 'jj.lara', 'jm.luna', 'jm.perilla', 'jm.vargas12', 'jn.zamora',
                         'jp.bohorquez11', 'jp.figueroa', 'jp.garces', 'jp.giraldoq', 'jp.gonzalez30',
                         'jr.melendez', 'js.garciad1', 'js.guzman', 'js.martinezf', 'js.rozo', 'ka.valencia10',
                         'km.rico', 'kt.noguera', 'kv.castellanos', 'kv.hernandez', 'l.fuentesl', 'l.pintop',
                         'la.arenas', 'la.herrera11', 'la.jimenezr', 'la.sierra', 'la.zamudio10', 'lc.garzonv',
                         'lc.jimenezl', 'lc.velasco', 'lf.cuellar', 'lf.gonzalez', 'lf.maldonado', 'lf.urrego10',
                         'li.amaya', 'lj.rincon', 'lk.lizarazo10', 'lm.rodriguez16', 'lm.rodriguezr', 'ls.daza10',
                         'ls.quintero', 'lt.granados10', 'lt.pedraza10', 'lv.arizag', 'lv.vargass', 'lv.villamil',
                         'm.arevalos', 'm.ayalde', 'm.camacho132', 'm.jimenezc', 'm.ortegac', 'm.prieto11',
                         'ma.francot',
                         'ma.morenor', 'ma.orduz10', 'ma.ulloa', 'ma.vargas11', 'mc.tavera', 'mc.zambrano10',
                         'mj.romerov', 'mp.escobar', 'mp.gonzalezr1', 'mp.reyes', 'ms.zarate', 'mv.garcia',
                         'mv.rodriguez1', 'n.bahamon', 'n.calderonr', 'n.cardenas12', 'n.castilla', 'n.jacome',
                         'n.medinal1', 'n.paezs', 'n.palacios', 'na.delgado', 'na.erazo', 'na.morenoe', 'na.salazar',
                         'na.vergara', 'nv.fonseca', 'oa.hernandez12', 'od.cuestas', 'oj.taboada', 'p.arenas',
                         'p.galindom', 'p.pastranav', 'pa.pinzon', 'pa.vargas', 'pb.hernandez10', 'pc.vargasm',
                         'ra.nieto', 's.becerra', 's.blanco13', 's.marind', 's.morenom1', 's.tangarife',
                         'sc.machetts10',
                         'se.cardenas', 'se.saturno', 'sj.varela', 'sm.mora12', 'sv.hernandez', 't.barragan10',
                         't.velez', 'v.gutierrez', 'v.munozc', 'v.reyes13', 'v.saldana', 'v.velez', 'vn.rodriguez',
                         'we.simancas10', 'ya.bejarano10', 'yc.rodriguez11', 'a.castaneda', 'a.gonzalez167',
                         'ae.silva10', 'af.hernandezl1', 'as.parraga10', 'd.duenasc', 'd.garcia23', 'd.guiza10',
                         'dr.castrillo', 'g.peralta', 'j.ardilas', 'ja.garcia921', 'jd.barrientos', 'jd.herrerac',
                         'jf.bermudez12', 'jm.guerrero2479', 'jm.nadera', 'js.molina12', 'js.rodriguezb1',
                         'js.torres17',
                         'kv.botero', 'lf.pinzon', 'm.gomezs', 'm.sanchez155', 'mj.hernandezp', 'ml.zambrano',
                         'mm.cano',
                         'n.hernandezg', 's.devia10', 's.hurtado10', 'sg.forero', 'sg.quiroga', 'vv.bautista',
                         'obecerra', 'ndcaro',
                         'sneira10', 'esanz', 'srivera', 'jaguerra', 'abrosero']

        vector_payoff = [50000, 35000, 17000, 35000, 35000, 25000, 25000, 50000, 25000, 35000, 35000, 25000, 35000,
                         25000, 35000, 33000, 45000, 35000, 35000, 35000, 25000, 50000, 50000, 43000, 35000, 25000,
                         25000, 40000, 25000, 35000, 35000, 35000, 28000, 25000, 25000, 25000, 28000, 40000, 40000,
                         25000, 20000, 50000, 25000, 33000, 25000, 35000, 35000, 33000, 35000, 28000, 25000, 25000,
                         35000, 25000, 35000, 25000, 25000, 35000, 25000, 35000, 15000, 25000, 25000, 35000, 32000,
                         35000, 40000, 35000, 25000, 35000, 25000, 25000, 10000, 35000, 35000, 35000, 28000, 25000,
                         35000, 35000, 25000, 43000, 25000, 40000, 35000, 25000, 35000, 35000, 50000, 25000, 35000,
                         35000, 30000, 35000, 25000, 35000, 25000, 35000, 32000, 35000, 25000, 38000, 28000, 25000,
                         35000, 25000, 25000, 33000, 35000, 30000, 25000, 28000, 35000, 35000, 35000, 28000, 45000,
                         35000, 43000, 25000, 25000, 32000, 25000, 35000, 45000, 35000, 43000, 35000, 25000, 35000,
                         35000, 25000, 45000, 35000, 43000, 33000, 35000, 35000, 27000, 25000, 30000, 43000, 50000,
                         25000, 35000, 50000, 43000, 35000, 25000, 25000, 35000, 25000, 20000, 25000, 30000, 38000,
                         35000, 25000, 43000, 17000, 25000, 40000, 35000, 35000, 30000, 35000, 35000, 40000, 35000,
                         35000, 10000, 10000, 40000, 50000, 35000, 43000, 40000, 35000, 43000, 25000, 35000, 35000,
                         25000, 25000, 25000, 40000, 25000, 25000, 25000, 25000, 50000, 50000, 50000, 50000, 50000,
                         50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000,
                         50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000, 50000,
                         50000, 50000, 44000, 22000, 22000, 55000, 45000, 35000, 50000]

        for x in range(0, len(vector_labels)):
            if self.participant.label == vector_labels[x]:
                positiony = x

        valor = vector_payoff[positiony]

        return dict(valor=valor)


class S7_2Payment(Page):
    form_model = 'player'

    def get_form_fields(self):
        if self.player.typepayment:
            return ['bank', 'id_bank', 'name_2', 'account_type', 'idcard3']
        else:
            return ['platform', 'telephone_2']


class S8_Thanks(Page):
    pass


page_sequence = [
    S7_1Payment,
    S7_2Payment,
    S8_Thanks
]
