import base64
import math
import re
from urllib.parse import quote, unquote


def wv(t, e, n, r, o):
    return math.floor(((t - e) / (n - e)) * (o - r) + r)
def left_shift(based, counter):
    val = based << (counter % 32)
    maxint = 2147483647
    if not -maxint - 1 <= val <= maxint:
        val = (val + (maxint + 1)) % (2 * (maxint + 1)) - maxint - 1
    return val


class PxUtils():
    def __init__(self):
        pass

    def encode_payload(self, payload, uuid, ts=''):
        sts = ts if len(ts) != 0 else '1604064986000'
        t1 = self.ie(payload, 50)
        # print(t1)
        BasePayload = self.encode(t1)
        # print(BasePayload)
        fv = self.ie(self.encode(sts), 10)
        # print(fv)
        t3 = self.Ev(fv, len(BasePayload), uuid)
        return self.Fa(fv, BasePayload, t3)
    def ie(self, t, e):
        t = t.encode()
        n = bytearray()
        for i in range(len(t)):
            n.append(e ^ t[i])
        return n
    def encode(self, t):
        encoded_str = quote(t)
        res = re.findall('(%[0-9A-F]{2})', encoded_str)
        for i in set(list(res)):
            # print(i, '0x'+i[1:])
            encoded_str = encoded_str.replace(i, chr(int('0x'+i[1:] , 16)))
        b64_encoded = base64.b64encode(encoded_str.encode('utf-8')).decode('utf-8')
        return b64_encoded

    def Ev(self, t, e, n):
        r = self.ie(self.encode(n), 10)
        o = []
        a = -1
        for i in range(len(t)):
            l = math.floor(i / len(r)+ 1)
            # print(l)
            if i >= len(r):
                c = i % len(r)
            else:
                c = i
            u = r[c] * r[l]
            # print(i, l, c, u)
            if u > a:
                a = u
        for f in range(len(t)):
            s = math.floor(f / len(r)) + 1
            d = f % len(r)
            p = r[d]*r[s]
            # print(f, s, d, p, p > e, wv(p, 0, a, 0, e-1))
            if p >= e:
                p = wv(p, 0,a,0,e-1)
            while p in o:
                p += 1
            o.append(p)

        o = sorted(o)
        return o

    def Fa(self, t, e, n):
        r = ''
        o = 0
        a = t.decode()
        for i in range(len(t)):
            # print(i,o,n[i], n[i] - i - 1, e[o: n[i] - i - 1], a[i], e[o: n[i] - i - 1]+a[i])
            r += e[o: n[i]-i-1] + a[i]
            o = n[i] - i - 1
            # print('iii',i,r)
            # print()
        # print(e[o:])
        r += e[o:]
        return  r


if __name__ == '__main__':
    p = PxUtils()
    payload = '[{"t":"PX12095","d":{"PX11645":"https://www.flyfrontier.com/","PX12207":0,"PX12458":"MacIntel","PX11902":0,"PX11560":3551,"PX12248":3600,"PX11385":1701250613545,"PX12280":1701250613554,"PX11496":"d4726a80-8e9a-11ee-b89f-0f4cdfae8973","PX12564":null,"PX12565":-1,"PX11379":false}}]'
    uuid = 'd4726a80-8e9a-11ee-b89f-0f4cdfae8973'
    print(p.encode_payload(payload, uuid))
    t = 'url, sid, vid, ts, num1, num2, str1, uuid, startTs, startTs2, performanceNow, px11280'
    for i in t.split(', '):
        print(f'self.{i} = {i}')