from loguru import logger
import logging
import pathlib
from pathlib import Path
import platform
import sys
import time
import traceback
import uuid
from concurrent.futures import ThreadPoolExecutor

import requests

from reese84_bypass import Reese84ByPass

work_dir = '/data/log/lcc-pr/'
if platform.platform().startswith('mac'):
    work_dir = '/Users/wang/Documents/company/lcc-reese84/'

if not pathlib.Path('nacos-data/snapshot/').exists():
    pathlib.Path('nacos-data/snapshot/').mkdir(parents=True)


def check_pull():
    get_url = 'http://ymx-lcc.unififi.com/header/task/pull/cookiePR?brush_name=test'
    r = requests.get(get_url)
    print(r.json())
    return r.json()


def get_proxy():
    get_proxy_url = 'http://ymx-lcc.unififi.com/proxy/get-proxy'
    r = requests.post(get_proxy_url, json={
        'name': 'PR',
        'type': 'shopping'
    })
    return r.json()


def add_header(headers: dict, cookies: dict, px: str, thread_id=0,log=None):
    cks = []
    for k, v in cookies:
        cks.append(f'{k}={v}')
    headers['cookie'] = '; '.join(cks)
    data = {
        "brush_fromdate": "2023-06-19",
        "brush_fromto": "BOS-JFK",
        "brush_header": headers,
        "brush_name": f"reverse_{thread_id}",
        "brush_proxy": px,
        "brush_type": "cookiePR"
    }
    r = requests.post("http://ymx-lcc.unififi.com/header/add", json=data)
    log.info(r.json())


def exec(thread_id):
    log = logger.bind(module_name=thread_id)
    # if not log.handlers:
    #     log.addHandler(stream_handler)
    log.remove()
    # log.add(sys.stdout,
    #              format='{time:YYYY-MM-DD HH:mm:ss.SSS} |<lvl>{level:8}</>| {name} : {module}:{line:4} | <cyan>{extra[module_name]}</> | - <lvl>{message}</>',
    #              colorize=True, level="INFO")

    log.add(pathlib.Path(work_dir).joinpath(f'pr-{thread_id}.log'),
            format='{time:YYYY-MM-DD HH:mm:ss.SSS} |<lvl>{level:8}</>| {name} : {module}:{line:4} | <cyan>{extra[module_name]}</> | - <lvl>{message}</>',
            colorize=False, level="INFO")
    log.warning(f"logpath {Path(work_dir).joinpath(f'pr-{thread_id}.log')}")
    while True:
        id_ = uuid.uuid4().hex
        pull = check_pull()
        try:
            if pull.get('status') == 1 and pull.get('message') != 'redis: nil':
                log.info('休眠十秒')
                time.sleep(10)
                continue
        except:
            pass

        try:
            px = get_proxy()['url']
            url = 'https://book.philippineairlines.com/plnext/paldx/Override.action;jsessionid=n6YrVmbx3EwcM5Jie7XnAz-esoQV25sPoRjirj-Z!1706081876056?BOOKING_FLOW=REVENUE&COUNTRY_SITE=GB&EMBEDDED_TRANSACTION=FlexPricerAvailability&ENC=0FBFA38B296A737C8F0EADFF17E094BD6790BAA37DE41FDC0EC4BC81AA6746E4E4D14DB117CBA73BA1879DB46C0BBB110992DA9F4BBB5C8904F10B533C05A2369471290FA22873719B4ED9EEDD95049F7F8FBE19EF98D3650173B96A6663DECC2B9A3AC903FD90794EC0D102A98AF88339038713DD29BA92CA5A5B0035E5399CE75A635754AC9FDEF661AEDFD86B80C092419E615043FC129FF0116B21059CA29C3515377F6303DD23B329E1DE6AAA85922B27DD33D7C3A8CCF9210CF43E830D2A9514607FD86680FAA4D40D05EFE2FA9168F57D0EF5F64DD3E8ECA949B4AF13687D92951BF231AC137B746E3C4D9F740A3B02283E97F9E5005750C38262AD9EFF4F1EC159089125D70038BD8264E730AC8A14A102C06A2EE2C71649502667B42F75BA8CBBA9C0D417725D3A9ED2498AE9645F757A10C1692696E724DA6D9BD194EA6544169AC5B1C6F483A44809904359241755918E74F56AB0F289A94E7161AF10B16EDCBAC8914DC9C1D71EE0AFDDE277126C0E9AA01E2473409664231161303303F0DC3819B87B646240CEB3596FEF02D0DF7EAD50EBD8425C256867167BB00FE5EBAB71D1426693CC133BA723587E415CAE7262B10A50B6037A8FACF7E630C3E02EFA59D0A0F4AEFC3191250209223DD2A6240B9A55D4EC22182F348CA4D4A1F26CF23E1E09DB1F0B7D2D42D84E9F73DAE511D01514E3EF0B78E685423CE3C22CD1B3BB26D569120CB897D28A26FD233466D0308DD6A024FF4573AEF705DB97E343D89E730764CFB5A8A79418DD25F599861A96F67B785B8C7E3F76AD92B2CA6766ABB34FC30A787614118F25638FA4ACADC164E0F7E033C568F03A9961A5B41CAD6A33961EB3EA3F7B052B8D87AE0C20E23794384B6B7807A50CB9C675803121D5B20CF41E1DF0984B65EB667F612063981AFF06BD32DFCEA9E9B627E0E162957981E51B008069C135D12E8BAA43993483854E459DAE6744B8E1DCB8950E905A51376D178EE7D96D326FDAD3498D1EEEA96316B334948FE5BA1497FE151E25AF950D9BFA01746559166620701409C3586D55968B48CD863F15899D49E6ED4B4EBA119D9D8CEAE7BF9F70F5234327390E7D1AA8660D907F7E5EFD46618AEC39BA6BA4B4E6BDA86546285EB51B65077F2F81F82C18480F6B515252E5ED78BC250E3B6EE7640AE8342A3566E550825407E2CE8E77D3284B42F732ACC56C9428C1E36B86C3E5F268D798E08E3C981C05793FFABABB7210D7ECEFB799BD8146CFD41087FB75D010155CFFF92984609C665DF06989433A3BC7B4C1D396CA99A565F48070C106F33999A505BB6A0A6AF0B57F01F8E5210BF9B275B052821144B8&ENCT=1&EXTERNAL_ID=BOOKING&LANGUAGE=GB&OFFICE_ID=SFOPR08WB&PAGE_ID=FDCT&SITE=O01DO01D&TRIP_FLOW=YES&_t=1706081887'
            cf = Reese84ByPass(target=url, proxy=px, thread_name=f'{thread_id}',log=log)
            headers, cookies = cf.start()
            # cf.test()
            add_header(headers, cookies, px, thread_id, log=log)
        # except json.decoder.JSONDecodeError:
        #     print('json huoqu shibai ')
        except Exception as e:
            log.error(e)
            traceback.print_exc()


def run():
    brush_thread = 2

    print(f'本次并发数 {brush_thread}')

    # pool = ThreadPoolExecutor(max_workers=brush_thread)
    # all_task = [pool.submit(exec, (i)) for i in range(brush_group)]
    # brush_check = CONFIG['internal']['reverse']['making']['brush_check']
    # wait(all_task, return_when=ALL_COMPLETED)
    # print(f'休息 {brush_check} 秒')
    # time.sleep(brush_check)

    with ThreadPoolExecutor(max_workers=brush_thread) as executor:
        objs = []
        for t in range(1, brush_thread + 1):
            task = executor.submit(exec, t)
            objs.append(task)
        while 1:
            time.sleep(1)
            for n, o in enumerate(objs):
                if o.done():
                    task = executor.submit(exec, n + 1)
                    objs[n] = task


if __name__ == '__main__':
    run()
