# -*- coding: utf-8 -*-
#
#   dp for Tornado
#     YoungYong Park (youngyongpark@gmail.com)
#     2014.10.28
#


import tornado.web

from engine.engine import Engine as dpEngine


class Pagination(tornado.web.UIModule, dpEngine):
    region_tag = 'div'
    region_class = 'pagination'

    prev = '<span></span> Prev'
    prev_class = 'direction prev'

    next = 'Next <span></span>'
    next_class = 'direction next'

    current_tag = 'strong'
    current_class = ''

    link_tag = 'a'
    link_class = ''

    space = ''

    def render(self, total_count, page, rpp, **options):
        rpb = options['rpb'] if 'rpb' in options else 10
        space = options['space'] if 'space' in options else self.space
        prev = options['prev'] if 'prev' in options else self.prev
        next = options['next'] if 'next' in options else self.next

        uri = self.helper.url.parse(self.request)
        url = uri.path
        params = uri.query

        last_page = self.helper.math.ceil(total_count * 1.0 / rpp * 1.0)

        if page < 1:
            page = 1
        elif page > last_page:
            page = last_page

        current_block = int(self.helper.math.ceil(page * 1.0 / rpb * 1.0))
        last_block = int(self.helper.math.ceil(last_page * 1.0 / rpb * 1.0))

        current_block = int(current_block)
        last_block = int(last_block)

        output = ''
        output = '%s<%s class="%s">' % (output, self.region_tag, self.region_class)

        # Prev Button
        if current_block > 1:
            params['page'] = (current_block - 1) * rpb
            s = self.helper.url.build(url, params)
            output = ('%s<%s href="%s" class=%s">%s</%s>%s'
                      % (output, self.link_tag, s, self.prev_class, prev, self.link_tag, space))

        # Paging
        for i in range(((current_block - 1) * rpb) + 1, (current_block * rpb) + 1):
            if i < 1:
                continue
            elif i > last_page:
                break

            if i != ((current_block - 1) * rpb) + 1:
                output = '%s%s' % (output, space)

            if page == i:
                output = ('%s<%s class="%s">%s</%s>'
                          % (output, self.current_tag, self.current_class, i, self.current_tag))
            else:
                params['page'] = i
                s = self.helper.url.build(url, params)

                output = ('%s<%s%s href="%s">%s</%s>'
                          % (output, self.link_tag, self.link_class, s, i, self.link_tag))

        # Next Button
        if current_block < last_block:
            params['page'] = (current_block * rpb) + 1
            params['page'] = last_page if params['page'] > last_page else params['page']
            s = self.helper.url.build(url, params)

            output = ('%s%s<%s href="%s" class="%s">%s</%s>'
                      % (output, space, self.link_tag, s, self.next_class, next, self.link_tag))

        return output
@@ -498,24 +498,45 @@
 k_class = ''
+%0A    link_prefix = ''
 %0A%0A    space 
@@ -655,16 +655,214 @@
 else 10%0A
+        current_tag = options%5B'current_tag'%5D if 'current_tag' in options else self.current_tag%0A        current_class = options%5B'current_class'%5D if 'current_class' in options else self.current_class%0A
         
@@ -936,16 +936,20 @@
     prev
+_btn
  = optio
@@ -1007,16 +1007,20 @@
     next
+_btn
  = optio
@@ -1065,16 +1065,111 @@
 elf.next
+%0A        link_prefix = options%5B'link_prefix'%5D if 'link_prefix' in options else self.link_prefix
 %0A%0A      
@@ -1966,32 +1966,34 @@
 ('%25s%3C%25s href=%22%25s
+%25s
 %22 class=%25s%22%3E%25s%3C/
@@ -2038,32 +2038,45 @@
 , self.link_tag,
+ link_prefix,
  s, self.prev_cl
@@ -2084,16 +2084,20 @@
 ss, prev
+_btn
 , self.l
@@ -2105,21 +2105,18 @@
 nk_tag, 
-space
+''
 ))%0A%0A    
@@ -2140,23 +2140,13 @@
     
-for i in range(
+ra = 
 ((cu
@@ -2172,17 +2172,29 @@
 rpb) + 1
-,
+%0A        rb =
  (curren
@@ -2211,16 +2211,47 @@
 rpb) + 1
+%0A%0A        for i in range(ra, rb
 ):%0A     
@@ -2366,50 +2366,95 @@
  if 
-i != ((current_block - 1) * rpb) + 1:%0A    
+ra == i:%0A                _space = ''%0A            else:%0A                _space = space%0A%0A
     
@@ -2484,24 +2484,25 @@
  %25 (output, 
+_
 space)%0A%0A    
@@ -2613,21 +2613,16 @@
 output, 
-self.
 current_
@@ -2626,21 +2626,16 @@
 nt_tag, 
-self.
 current_
@@ -2644,21 +2644,16 @@
 ass, i, 
-self.
 current_
@@ -2806,24 +2806,26 @@
 s%25s href=%22%25s
+%25s
 %22%3E%25s%3C/%25s%3E'%0A 
@@ -2891,16 +2891,29 @@
 k_class,
+ link_prefix,
  s, i, s
@@ -3224,16 +3224,18 @@
 href=%22%25s
+%25s
 %22 class=
@@ -3281,21 +3281,18 @@
 output, 
-space
+''
 , self.l
@@ -3299,16 +3299,29 @@
 ink_tag,
+ link_prefix,
  s, self
@@ -3337,16 +3337,20 @@
 ss, next
+_btn
 , self.l

Add more options for pagination.