/** @preserve
 * Hit Kounter Help script v0.1
 * Home: https://github.com/zry656565/Hit-Kounter
 * Author: Jerry Zou
 * Email: jerry.zry@outlook.com
 */

'use strict'

import Icarus from './icarus.js'

require('../../styles/index.less')

let TEMPLATES= {
  TOP_AREA: require('../../templates/top-area.jade')
}

let HitKounter = {
  elements: {},
  scan() {
    let e = this.elements
      , pages = document.querySelectorAll('[data-hk-page]')

    e.current = document.querySelectorAll('[data-hk-page=current]')
    e.topArea = document.querySelectorAll('[data-hk-top-pages]')
    e.pages = new Map()
    for (let i = 0; i < pages.length; ++i) {
      let url = pages[i].attributes['data-hk-page'].value
        , arr = e.pages.get(url)
      if (url == 'current') { continue }
      if (arr) { arr.push(pages[i]) }
      else { e.pages.set(url, [pages[i]]) }
    }
  },
  increment() {
    let {elements} = this
    Icarus.request({
      api: 'hk.page.increment',
      v: '1.0',
      success(result) {
        for (let i = 0; i < elements.current.length; ++i) {
          elements.current[i].innerText = result.count
        }
      },
      failure(code, err) { console.log(code, err) }
    })
  },
  getPages() {
    let {elements} = this
    let pagesParam = []

    for (let iter = elements.pages.keys(), state = iter.next(); !state.done; state = iter.next()) {
      pagesParam.push({ url: state.value })
    }

    Icarus.request({
      api: 'hk.page.get',
      v: '1.0',
      pages: pagesParam,
      success(results) {
        for (let i = 0; i < results.length; ++i) {
          let arr = elements.pages.get(results[i].url)
          for (let j = 0; j < arr.length; ++j) {
            arr[j].innerText = results[i].count
          }
        }
      },
      failure(code, err) { console.log(code, err) }
    })
  },
  getTop() {
    let {elements} = this
    let topNum = elements.topArea[0].attributes['data-hk-top-pages'].value

    Icarus.request({
      api: 'hk.page.getTop',
      v: '1.0',
      num: topNum,
      success(results) {
        let topAreaDom = TEMPLATES.TOP_AREA({
          pages: results,
          num: topNum
        })
        for (let i = 0; i < elements.topArea.length; ++i) {
          elements.topArea[i].innerHTML = topAreaDom
        }
      },
      failure(code, err) { console.log(code, err) }
    })
  }
}

window.onload = function() {
  var hk = HitKounter
    , e = hk.elements

  hk.scan()
  hk.increment()
  if (e.pages.size > e.current.length) hk.getPages()
  if (e.topArea.length) hk.getTop()
}

window.HitKounter = HitKounter
export default HitKounter
@@ -1261,16 +1261,103 @@
 ram = %5B%5D
+%0A    const LIMITATION = 800   // prevent length of request from being greater than 1024
 %0A%0A    fo
@@ -1498,19 +1498,269 @@
  %7D)%0A    
-%7D%0A%0A
+  if ( encodeURIComponent(JSON.stringify(pagesParam)).length %3E LIMITATION ) %7B%0A        getPartialPages(pagesParam)%0A        pagesParam = %5B%5D%0A      %7D%0A    %7D%0A%0A    if (pagesParam.length %3E 0) getPartialPages(pagesParam)%0A%0A    function getPartialPages(pages) %7B%0A  
     Icar
@@ -1764,32 +1764,34 @@
 carus.request(%7B%0A
+  
       api: 'hk.p
@@ -1798,32 +1798,34 @@
 age.get',%0A      
+  
 v: '1.0',%0A      
@@ -1814,24 +1814,26 @@
   v: '1.0',%0A
+  
       pages:
@@ -1842,21 +1842,16 @@
 ages
-Param
 ,%0A      
 succ
@@ -1838,32 +1838,34 @@
 s: pages,%0A      
+  
 success(results)
@@ -1859,32 +1859,34 @@
 cess(results) %7B%0A
+  
         for (let
@@ -1926,24 +1926,26 @@
 %7B%0A          
+ 
+ 
 let arr = el
@@ -1979,32 +1979,34 @@
 .url)%0A          
+  
 for (let j = 0; 
@@ -2040,16 +2040,18 @@
         
+ 
+ 
 arr%5Bj%5D.i
@@ -2084,26 +2084,30 @@
 t%0A          
-%7D%0A
+  %7D%0A  
         %7D%0A  
@@ -2102,35 +2102,39 @@
         %7D%0A      
+  
 %7D,%0A
+  
       failure(co
@@ -2165,34 +2165,42 @@
 ode, err) %7D%0A    
+  %7D)%0A    
 %7D
-)
 %0A  %7D,%0A  getTop()

split `hk.pages.get` if the length of request is too long