"""Ranking retrieval."""
import json

from common import _, _open_url
from index import index


def _f_id():
    """The identifier for a given mode and name."""
    def _id(mode, name):
        if mode == _.STAR:
            url  = _.STAR_ID_URL
            key  = _.DISC_KEY
            data = star_index
        elif mode == _.POP:
            url  = _.POP_ID_URL
            key  = _.DISC_KEY
            data = pop_index
        elif mode == _.CLUB:
            url  = _.CLUB_ID_URL
            key  = _.CLUB_KEY
            data = club_index
        elif mode == _.MISSION:
            url  = _.MISSION_ID_URL
            key  = _.MISSION_KEY
            data = mission_index
        else:
            raise ValueError("invalid game mode")
        for record in json.loads(_open_url(url.format(data[name]["page"]), "retrieving ID").read().decode())["DATA"]["RECORD"]:
            if record[key["name"]] == name:
                return record[key["id"]]

    star_index    = index(_.STAR)
    pop_index     = index(_.POP)
    club_index    = index(_.CLUB)
    mission_index = index(_.MISSION)
    return _id


def ranking(mode, name, chart="nm"):
    """The complete ranking of the specified mode and name.

    Any of the four game mode constants defined in the common module can be
    given as the first argument.  The name must be the complete name of a disc,
    disc set, or mission.  The chart is a key from the CHART dictionary defined
    in the common module.  It is only relevant to Pop Mode, and defaults to the
    value of the "nm" key.

    """
    if mode == _.STAR:
        url = _.STAR_RANKING_URL
    elif mode == _.POP:
        url = _.POP_RANKING_URL
    elif mode == _.CLUB:
        url = _.CLUB_RANKING_URL
    elif mode == _.MISSION:
        url = _.MISSION_RANKING_URL
    else:
        raise ValueError("invalid game mode")
    page = 1
    results = []
    identifier = _id(mode, name)
    while True:
        reply = json.loads(_open_url(url.format(identifier, page) + (("&pt=" + _.CHART[chart]) if mode == _.POP else ""), "retrieving rankings").read().decode())["DATA"]["RECORD"]
        results.extend((record["RANK"], record["DJICON"], record["DJNAME"], record["SCORE"]) for record in reply)
        if len(reply) < 20:
            break
        page += 1
    return results


_id = _f_id()

@@ -93,22 +93,30 @@
 x%0A%0A%0Adef 
-_f
 _id(
+mode, name
 ):%0A    %22
@@ -166,37 +166,8 @@
 %22%22%22%0A
-    def _id(mode, name):%0A    
     
@@ -185,36 +185,32 @@
 _.STAR:%0A        
- 
-   
 url  = _.STAR_ID
@@ -214,36 +214,32 @@
 _ID_URL%0A        
-    
 key  = _.DISC_KE
@@ -252,34 +252,29 @@
     
-   
- 
 data = 
-star_index%0A    
+index(_.STAR)%0A
     
@@ -293,36 +293,32 @@
  _.POP:%0A        
-  
-  
 url  = _.POP_ID_
@@ -321,36 +321,32 @@
 _ID_URL%0A        
-    
 key  = _.DISC_KE
@@ -343,28 +343,24 @@
  _.DISC_KEY%0A
-    
         data
@@ -366,22 +366,21 @@
 a = 
-pop_
 index
-%0A    
+(_.POP)%0A
     
@@ -400,36 +400,32 @@
 _.CLUB:%0A        
-    
 url  = _.CLUB_ID
@@ -429,36 +429,32 @@
 _ID_URL%0A        
- 
-   
 key  = _.CLUB_KE
@@ -467,34 +467,29 @@
     
-    
 data = 
-club_
 index
+(_.CLUB)
 %0A
-    
     
@@ -516,28 +516,24 @@
 ON:%0A        
- 
-   
 url  = _.MIS
@@ -548,28 +548,24 @@
 URL%0A        
-  
-  
 key  = _.MIS
@@ -585,37 +585,32 @@
     
-    
 data = 
-mission_index%0A    
+index(_.MISSION)%0A
     
@@ -611,28 +611,24 @@
 )%0A    else:%0A
-    
         rais
@@ -661,20 +661,16 @@
  mode%22)%0A
-    
     for 
@@ -793,20 +793,16 @@
         
-   
- 
 if recor
@@ -837,20 +837,16 @@
         
-    
 return r
@@ -867,162 +867,8 @@
 %5D%5D%0A%0A
-    star_index    = index(_.STAR)%0A    pop_index     = index(_.POP)%0A    club_index    = index(_.CLUB)%0A    mission_index = index(_.MISSION)%0A    return _id%0A%0A
 %0Adef
@@ -2064,20 +2064,4 @@
 lts%0A
-%0A%0A_id = _f_id()%0A

Remove the closure.