from otree.api import *
import random

doc = """
This app is intended to facilitate the delivery of discrete choice experiments & conjoint analysis by offering the capability to 
generate randomized choice sets
"""


class C(BaseConstants):
    NAME_IN_URL = 'ESG_DCE'
    PLAYERS_PER_GROUP = None
    NUM_ROUNDS = 6


class Subsession(BaseSubsession):
    pass


class Group(BaseGroup):
    pass


class Player(BasePlayer): ##need to define their inputs, but also all data i want stored (including make up of the choice sets), as i need that in the data file
    choices = models.StringField(label="Which fund would you rather purchase?", choices=[['A', 'A'], ['B', 'B']], widget=widgets.RadioSelectHorizontal) #actual choice between choice 1 or choice 2
    attribute1level1 = models.StringField()
    attribute2level1 = models.StringField() ##choices is the choice they made, everything else is to store choice informationred
    attribute3level1 = models.StringField()
    attribute4level1 = models.StringField()
    attribute5level1 = models.StringField()
    attribute6level1 = models.StringField()
    attribute7level1 = models.StringField()
    attribute1level2 = models.StringField()
    attribute2level2 = models.StringField()
    attribute3level2 = models.StringField()
    attribute4level2 = models.StringField()
    attribute5level2 = models.StringField()
    attribute6level2 = models.StringField()
    attribute7level2 = models.StringField()
    attribute8level1 = models.StringField()
    attribute8level2 = models.StringField()
    rating1 = models.IntegerField(label ="On a scale of 1-7, how appealing do you find Fund A?", choices=[1, 2, 3, 4, 5, 6, 7],widget=widgets.RadioSelectHorizontal) #7 point likert rating for choice 1
    rating2 = models.IntegerField(label ="On a scale of 1-7, how appealing do you find Fund B?", choices=[1, 2, 3, 4, 5, 6, 7],widget=widgets.RadioSelectHorizontal) #7 point likert rating for choice 2
    prolific_ID = models.StringField()


# PAGES
class IntroPage(Page):
    pass

class Choice(Page):
    form_model = "player"
    form_fields = ["choices", "rating1", "rating2"]
    @staticmethod
    def vars_for_template(player: Player): #this is the randomizer
        attribute1 = ['XYZ Equity Fund', 'XYZ ESG Equity Fund', 'XYZ Sustainable Equity Fund', 'XYZ SRI Equity Fund']  ## create each attribute as a list, with each level as a value
        attribute2 = ['This profesionally managed fund provides exposure to a diversified mix of securities designed to deliver year over year growth.', 'This profesionally managed fund provides exposure to a diversified mix of securities with Environmental, Social, and Governance characteristics.', 'This profesionally managed fund provides exposure to a diversified mix of securities that are focused on driving positive Environmental, Social, and Governance change.']
        attribute3 = ['This fund is not ESG Rated', 'B', 'C', 'star placeholder 1', 'star placeholder 2']
        attribute4 = ['','This rating measures how well a portfolio, and its holdings, are performing based on Environemntal, Social, and Governance factors in comparison to its peer group. This measure ranges from A (leader), B, C, D to F (laggard)', 'This rating measures how well a portfolio, and its holdings, are performing based on Environemntal, Social, and Governance factors in comparison to its peer group. This measure ranges from 5 stars (leader), 4, 3, 2 to 1 star (laggard)']
        attribute5 = ['This fund aims to increase the value of your investment by investing in a broad range of equity securities. Equities are selected that trade below their intrinsic value, demonstrate superior earnings growth and positive price momentum.','The funds investment selection process incorporates ESG data of the underlying issuers, namely the analysis of environmental, social, and governance factors alongside traditional financial analysis.', 'This fund uses general ESG integration, positive screening, and exclusionary screening by industry. Any industries involved in activities that do not align with the Environmental, Social, and/or Governance values are excluded from the funds holdings.']
        attribute6 = ['Medium-Low', 'Medium-High']
        attribute7 = ['<img src="https://imgpile.com/images/T8Xl6R.png" alt="T8Xl6R.png" border="0" />', '<img src="https://imgpile.com/images/T8Xzcc.png" alt="T8Xzcc.png" border="0" />']
        attribute8 = ['1.8', '2.7']
         # manually adjust below to reflect listed attributes above
        choiceuniverse = [attribute1, attribute2, attribute3, attribute4, attribute5, attribute6, attribute7, attribute8]  ##create a list containing all possible attributes & levels
        choiceset = []
        counter = 0 ## i'ma be honest - not sure why we're using a counter. will verify that its not necessary at some point because i don't think it is
        for i in choiceuniverse:
            counter += 1
            rand = random.randint(0,(len(i)-1))
            selected = i[rand]
            if i == attribute4: ##this is janky but it works. it is specific to this project and should be removed if this code is repurposed for other DCEs
                if selected != '':
                    if choiceset[4] == 'B' or choiceset[4] == 'C':
                        selected = i[1]
                    elif choiceset[4] == 'star placeholder 1' or choiceset[4] == 'star placeholder 2':
                        selected = i[2]
                    elif choiceset[4] == 'This fund is not ESG Rated':
                        selected = i[0]
            choiceset.append(selected)
            counter += 1 # run it twice as we'll need two for each attribute
            rand = random.randint(0, (len(i) - 1))
            selected = i[rand]
            if i == attribute4:
                if selected != '':
                    if choiceset[5] == 'B' or choiceset[5] == 'C':
                        selected = i[1]
                    elif choiceset[5] == 'star placeholder 1' or choiceset[5] == 'star placeholder 2':
                        selected = i[2]
                    elif choiceset[5] == 'This fund is not ESG Rated':
                        selected = i[0]
            choiceset.append(selected)
        result = {"attribute1level1": choiceset[0],
                  "attribute1level2": choiceset[1],
                  "attribute2level1": choiceset[2],
                  "attribute2level2": choiceset[3],
                  "attribute3level1": choiceset[4],
                  "attribute3level2": choiceset[5],
                  "attribute4level1": choiceset[6],
                  "attribute4level2": choiceset[7],
                  "attribute5level1": choiceset[8],
                  "attribute5level2": choiceset[9],
                  "attribute6level1": choiceset[10],
                  "attribute6level2": choiceset[11],
                  "attribute7level1": choiceset[12],
                  "attribute7level2": choiceset[13],
                  "attribute8level1": choiceset[14],
                  "attribute8level2": choiceset[15]
                  }
        return result

class Results(Page):
    pass


page_sequence = [IntroPage, Choice, Results]
