from pyscript import window, fetch
from puepy import Application, Page, t
from puepy.router import Router, Route
from js import URLSearchParams

app = Application()
app.install_router(Router, link_mode=Router.LINK_MODE_HASH)

@app.page('/')
class Root(Page):
    def initial(self):
        return {'user':None}

    def populate(self):
        t.p('User Homepage is unavailable, if you are attempting to verify with the PTC_PAM bot go to /verify')

@app.page('/verify') #theres no way this is even slightly optimized
class Verify(Page):
    def initial(self):
        return {'hs': False, 'ht': 'To Verify (or re-verify) : Click a Link button, Follow instructions, Click other Link button, Follow instrustions'}

    def oauthdisc(self, event=None):
        window.location.href = await fetch('https://api.tre-connector.playit.plus/oauth/discord', method='GET').text()

    def oauthrobl(self, event=None):
        window.location.href = await fetch('https://api.tre-connector.playit.plus/oauth/roblox', method='GET').text()

    def hmf(self, event=None):
        if self.state['hs'] == False:
            self.state['hs'] = True
        elif self.state['hs'] == True:
            self.state['hs'] = False

    def populate(self):
        with t.div():
            with t.div():
                t.button('Discord Link', on_click=self.oauthdisc)
            with t.div():
                t.button('Roblox Link', on_click=self.oauthrobl)
            with t.div(style='margin: 1em'):
                t.button('Help Menu', on_click=self.hmf)

        with t.div():
            if self.state['hs'] == False:
                t.span()
            elif self.state['hs'] == True:
                text = self.state['ht']
                with t.div(style='margine: 1em'):
                    t.span(text)

@app.page('/privacy') #hope i dont get sued
class Privacy(Page):
    def populate(self):
        with t.div():
            t.h2('Privacy Policy')
        with t.div():
            t.h3('This site can only access the following when used : Discord & Roblox UserID (stored), Discord Displayname & Username (not stored), Roblox Username & Displayname (not stored), Roblox & Discord Oauth Tokens (deactivated & removed once verification is completed)')
        with t.div():
            t.span('Discord Privacy Policy : https://discord.com/privacy')
        with t.div():
            t.span('Roblox Privacy Policy : https://en.help.roblox.com/hc/en-us/articles/115004630823-Roblox-Privacy-and-Cookie-Policy')

@app.page('/terms') #hope i dont get sued electric 2 whatever the rest of it is
class Terms(Page):
    def populate(self):
        with t.div():
            t.h2('Terms of Service')
        with t.div():
            t.h3('By Logging into or otherwise using the site you aggree to the following')
        with t.div():
            t.span('Do not Attempt to Get the web servers IP address or intentionally get the pages ratelimited')
        with t.div():
            t.span('Follow Roblox & Discords Terms Of service')
        with t.div():
            t.span('Discord Terms of service : https://discord.com/terms')
        with t.div():
            t.span('Roblox Terms of service : https://en.help.roblox.com/hc/en-us/articles/115004647846-Roblox-Terms-of-Use')

@app.page('/oauth/roblox') #I HATE OAUTH I HATE OAUTH I HATE OAUTH I HATE OAUTH I HATE OAUTH
class Roblox_Redirect(Page):
    def initial(self):
        q_dirty = window.location.hash
        q_clean = q_dirty.split('?')[1]
        data_new = URLSearchParams.new(q_clean)
        code_get = data_new.get('code')
        state_get = data_new.get('state')
        return {'code': code_get, 'state': state_get}

    def populate(self):
        with t.div():
            t.span('REDIRECT PROCESSING, REFRESH PAGE IF YOU ARENT SENT TO VERIFY PAGE OR FINAL PAGE WITHIN 20 SECONDS')

        ls = window.localStorage
        code_use = self.state['code']
        state_use = self.state['state']
        if ls.getItem('rblx_code') == None:
            ls.setItem('rblx_code',code_use)
            ls.setItem('rblx_state',state_use)
            if ls.getItem('disc_code') == None:
                window.location.href = 'https://tre-connector.playit.plus/#/verify'
            elif ls.getItem('disc_code') != None:
                window.location.href = 'https://tre-connector.playit.plus/#/oauth/final'
        elif ls.getItem('rblx_code') != None:
            if ls.getItem('disc_code') == None:
                window.location.href = 'https://tre-connector.playit.plus/#/verify'
            elif ls.getItem('disc_code') != None:
                window.location.href = 'https://tre-connector.playit.plus/#/oauth/final'

@app.page('/oauth/discord') #Its not that bad...
class Discord_Redirect(Page):
    def initial(self):
        q_dirty = window.location.hash
        q_clean = q_dirty.split('?')[1]
        data_new = URLSearchParams.new(q_clean)
        code_get = data_new.get('code')
        return {'code': code_get}

    def populate(self):
        with t.div():
            t.span('REDIRECT PROCESSING, REFRESH PAGE IF YOU ARENT SENT TO VERIFY PAGE OR FINAL PAGE WITHIN 20 SECONDS')

        ls = window.localStorage
        code_use = self.state['code']
        if ls.getItem('disc_code') == None:
            ls.setItem('disc_code',code_use)
            if ls.getItem('rblx_code') == None:
                window.location.href = 'https://tre-connector.playit.plus/#/verify'
            elif ls.getItem('rblx_code') != None:
                window.location.href = 'https://tre-connector.playit.plus/#/oauth/final'
        elif ls.getItem('disc_code') != None:
            if ls.getItem('rblx_code') == None:
                window.location.href = 'https://tre-connector.playit.plus/#/verify'
            elif ls.getItem('rblx_code') != None:
                window.location.href = 'https://tre-connector.playit.plus/#/oauth/final'

@app.page('/oauth/final') #Done with this crappy Oauth stuff (atleast for the verification pages)
class Oauth_End(Page):
    def initial(self):
        ls = window.localStorage
        disc_code_get = ls.getItem('disc_code')
        rblx_code_get = ls.getItem('rblx_code')
        rblx_state_get = ls.getItem('rblx_state')
        return {'disc_code': disc_code_get, 'rblx_code': rblx_code_get, 'rblx_state': rblx_state_get}

    def populate(self):
        disc_code = self.state['disc_code']
        rblx_code = self.state['rblx_code']
        rblx_state = self.state['rblx_state']
        url = 'https://api.tre-connector.playit.plus/oauth/redirect/' + disc_code + '/' + rblx_code + '/' + rblx_state
        resp = await fetch(url, method='GET').text()
        if resp == 'done':
            with t.div():
                t.span('50/50 chance you verified or my server ate more rocks')
        elif resp == 'fail':
                t.span('verification failed. try refreshing')

app.mount('#app')
