#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tornado.web
import requests
from common.base import Config
import json
from urllib.parse import unquote


class BaseController(tornado.web.RequestHandler):
    def render(self, tpl, **render_data):
        if not tpl.endswith('html'):
            tpl = "{}.html".format(tpl)
        super(BaseController, self).render(tpl, **render_data)


class My404(BaseController):
    def get(self):
        self.render('404')


def write_error(self, stat, **kw):
    # self.write('访问url不存在!')
    self.render('404.html')


class BaseRequestHandler(tornado.web.RequestHandler):
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')

    def initialize(self):
        pass


class initializeBaseRequestHandler(tornado.web.RequestHandler):
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')

    def initialize(self):
        self.verifyFlag = 0
        self.status = 1
        myUserId = self.get_arguments("UserID")
        myTokenId = self.get_arguments("TokenID")

        try:
            if (myUserId[0] and myTokenId[0]):
                self.verifyFlag = 1
                try:
                    self.UserId = myUserId[0]
                    self.TokenId = myTokenId[0]
                    URL = Config().get_content("isToken")["before_url"]
                    ret = requests.post(URL, data={"TokenId": self.TokenId, "UserId": self.UserId})
                    result = ret.text
                    self.status = json.loads(result)["status"]
                except Exception as e:
                    self.status = 1
        except:
            pass


class downBaseRequestHandler(tornado.web.RequestHandler):
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')

    def initialize(self):
        self.verifyFlag = 0
        self.status = 1
        myUserId = self.get_arguments("UserID")
        myTokenId = self.get_arguments("TokenID")
        # print(myUserId)
        # print(myTokenId)

        try:
            if (myUserId[0] and myTokenId[0]):
                self.verifyFlag = 1
                try:
                    self.UserId = myUserId[0]
                    self.TokenId = unquote(myTokenId[0])
                    # print(self.UserId)
                    # print(self.TokenId)
                    URL = Config().get_content("isToken")["before_url"]
                    ret = requests.post(URL, data={"TokenId": self.TokenId, "UserId": self.UserId})
                    result = ret.text
                    # print(result)
                    self.status = json.loads(result)["status"]
                    # print(self.status)
                except Exception as e:
                    self.status = 1
        except:
            pass
