# -*- coding: utf-8 -*-
# FILE: gists.py
# AUTHOR: pocari <caffelattenonsugar at gmail.com>
# License: MIT license

import urllib.request
import urllib.parse
import json

from denite import util
from .base import Base


GIST_BASE_URL = "https://api.github.com/users/{}/gists"


class Source(Base):

    def __init__(self, vim):
        super().__init__(vim)
        self.name = 'gists'
        self.kind = 'open_browser'


    def gather_candidates(self, context):
        gists_url = self._build_url(context)
        params = {
            "direction": "desc",
            "sort": "updated"
        }

        gists = self._get_gists(gists_url, params)
        if gists:
            return [self._convert(r) for r in gists]


    def _build_url(self, context):
        args = dict(enumerate(context['args']))
        arg = args.get(0)
        if arg:
            user_name = arg
        else:
            u = self.vim.call('denite_gists#util#github_user')
            if u:
                user_name = u
            else:
                user_name = util.input(self.vim, context, 'Gist User: ')

        return GIST_BASE_URL.format(user_name)


    def _get_gists(self, url, params):
        encoded_params = urllib.parse.urlencode(params)
        url = "{}?{}".format(url, encoded_params)
        try:
          with urllib.request.urlopen(url) as res:
              return json.loads(res.read().decode("utf-8"))
        except urllib.error.HTTPError as e:
            util.error(self.vim, "Error {} api url:{}".format(e.reason, url))


    def _convert(self, gist_info):
        return {
            "word": "{} ({})".format(gist_info["description"], gist_info["updated_at"]),
            "url": gist_info["html_url"]
        }


@@ -415,17 +415,16 @@
 owser'%0A%0A
-%0A
     def 
@@ -718,17 +718,16 @@
 gists%5D%0A%0A
-%0A
     def 
@@ -1136,17 +1136,16 @@
 _name)%0A%0A
-%0A
     def 
@@ -1304,16 +1304,18 @@
         
+  
 with url
@@ -1347,16 +1347,18 @@
 as res:%0A
+  
         
@@ -1532,17 +1532,16 @@
  url))%0A%0A
-%0A
     def 
@@ -1621,16 +1621,33 @@
 .format(
+%0A                
 gist_inf
@@ -1663,16 +1663,32 @@
 ption%22%5D,
+%0A               
  gist_in
@@ -1703,16 +1703,29 @@
 ted_at%22%5D
+%0A            
 ),%0A     
@@ -1760,16 +1760,16 @@
 l_url%22%5D%0A
+
         
@@ -1762,17 +1762,16 @@
 url%22%5D%0A        %7D%0A
-%0A

fix lint error