import json,os,re,sys,time,hashlib,urllib.request,urllib.parse,html
from concurrent.futures import ThreadPoolExecutor
OUT=sys.argv[1]; os.makedirs(OUT+"/pics",exist_ok=True)
UA={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0","Referer":"https://tds.fandom.com/"}
API="https://tds.fandom.com/api.php?"
def get(url,tries=5):
    for i in range(tries):
        try: return urllib.request.urlopen(urllib.request.Request(url,headers=UA),timeout=60).read()
        except Exception as e:
            if i==tries-1 or getattr(e,"code",0) in (404,410): raise
            time.sleep(3*(i+1))
def fname(t): return t.replace(" ","_").replace("/","__")+".html"
titles=[l.strip() for l in open(sys.argv[2]) if l.strip()]
mainpage="Tower Defense Simulator Wiki"
if mainpage not in titles: titles.insert(0,mainpage)
CSS="""body{font-family:sans-serif;max-width:1100px;margin:auto;padding:16px;background:#fff;color:#222}
a{color:#0645ad}img{max-width:100%;height:auto}table{border-collapse:collapse}td,th{border:1px solid #ccc;padding:4px}
.portable-infobox{float:right;width:270px;border:1px solid #ccc;margin:0 0 12px 12px;padding:6px;background:#f6f6f6}
.wds-tab__content{display:block!important}nav.top{margin-bottom:12px}@media(max-width:700px){.portable-infobox{float:none;width:auto;margin:0}}
@media(prefers-color-scheme:dark){body{background:#1b1b1b;color:#ddd}a{color:#8ab4f8}.portable-infobox{background:#262626;border-color:#444}td,th{border-color:#444}}"""
def img(m):
    u=html.unescape(m.group(0))
    u=re.sub(r"^//","https://",u)
    ext=os.path.splitext(urllib.parse.urlparse(u).path.split("/revision")[0])[1][:5] or ".png"
    n=hashlib.md5(u.encode()).hexdigest()+ext
    p=OUT+"/pics/"+n
    if not os.path.exists(p):
        try: tmp=p+".%d.tmp"%os.getpid()+str(id(m)); open(tmp,"wb").write(get(u)); os.replace(tmp,p)
        except Exception as e: print("IMGFAIL",u,e,flush=True); return u
    return "pics/"+n
def link(m):
    t=urllib.parse.unquote(m.group(1)).split("#"); frag=("#"+t[1]) if len(t)>1 else ""
    t=t[0].replace("_"," ")
    if ":" in t.split("/")[0] and not t.startswith(("Tower Defense",)): return 'href="https://tds.fandom.com/wiki/'+m.group(1)+'"'
    return 'href="'+urllib.parse.quote(fname(t))+frag+'"'
def do(t):
    f=OUT+"/"+fname(t)
    if os.path.exists(f): return
    try:
        d=json.loads(get(API+urllib.parse.urlencode({"action":"parse","page":t,"prop":"text|displaytitle","format":"json","redirects":1})))
        h=d["parse"]["text"]["*"]
    except Exception as e: print("FAIL",t,e,flush=True); return
    h=re.sub(r'<img([^>]*?)src="data:[^"]*"([^>]*?)data-src="([^"]+)"',r'<img\1src="\3"\2',h)
    h=re.sub(r'(?:https?:)?//static\.wikia\.nocookie\.net/[^"\s\']+',img,h)
    h=re.sub(r'href="/wiki/([^"?]+)"',link,h)
    h=re.sub(r'\s(srcset|data-src)="[^"]*"','',h)
    page=f'<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>{html.escape(t)}</title><style>{CSS}</style><nav class="top"><a href="index.html">TDS Arkivet</a> · <a href="alla-sidor.html">Alla sidor</a> · <a href="{urllib.parse.quote(fname(mainpage))}">Huvudsida</a></nav><h1>{d["parse"].get("displaytitle",html.escape(t))}</h1>{h}'
    open(f,"w").write(page)
with ThreadPoolExecutor(6) as ex:
    for i,_ in enumerate(ex.map(do,titles)):
        if i%100==0: print("klart",i,"/",len(titles),flush=True)
idx="".join(f'<li><a href="{urllib.parse.quote(fname(t))}">{html.escape(t)}</a></li>' for t in sorted(titles,key=str.lower))
open(OUT+"/alla-sidor.html","w").write(f'<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>TDS Wiki</title><style>{CSS}</style><p><a href="index.html">← TDS Arkivet</a></p><h1>Tower Defense Simulator Wiki (offline)</h1><p>Hämtad {time.strftime("%Y-%m-%d")} från tds.fandom.com. <a href="{urllib.parse.quote(fname(mainpage))}">Huvudsida</a></p><ul>{idx}</ul>')
print("KLART",flush=True)
