# -*- coding:utf-8 -*-
# Copyright 2018 Huawei Technologies Co.,Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use
# this file except in compliance with the License.  You may obtain a copy of the
# License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations under the License.

import base64
import requests
import time

retry_times_max = 3;
delay_time = 2.0

class HWOcrClientToken(object):
    """OCR Client authenticated by token

    initializd by username,domainname,passwrod,region,endpoint

    Attributes:
        domainname: domainname for ocr user.If not IAM user, it's the same with username
        region: region name for the ocr user,such as cn-north-1,cn-east-2
        password:password for the ocr user
        httpendpoint:httpendpoint for the ocr request
        token: temporary authentication key for the ocr user, expired after 24 hours
    """
    def __init__(self,domainname, username, password, region,endpoint):
        """
        Constructor for the HWOcrClientToken
        """
        if domainname=="" or username == "" or password == "" or region == "" or endpoint == "":
            raise ValueError('The parameter for the HWOcrClientToken constructor cannot be empty')
        self.domainname=domainname
        self.username = username
        self.password = password
        self.region = region
        self.httpendpoint=endpoint
        self.token = None
        self.repeatCount = 0

    def get_token_test(self):
        """
        get the token for the ocr user from IAM server
        :return:
        """
        # if self.token is not None:
        #     return self.token
        f = open("./data/tk.txt")
        line = f.readline()
        f.close()
        self.token = line;
        print(self.token)
        print(type(line))
        # self.token ='"[encoded data, 9120 characters]";'
        endpoint = 'iam.%s.myhuaweicloud.com' % self.region
        url = 'https://%s/v3/auth/tokens' % endpoint
        headers = {'Content-Type': 'application/json', 'X-Subject-Token':self.token,
            'X-Auth-Token':self.token}
        payload = {}
        # payload = {
        #     "auth": {
        #         "identity": {
        #             "methods": [
        #                 "password"
        #             ],
        #             "password": {
        #                 "user": {
        #                     "name": self.username,
        #                     "password": self.password,
        #                     "domain": {
        #                         "name": self.domainname
        #                     }
        #                 }
        #             }
        #         },
        #         "scope": {
        #             "project": {
        #                 "name": self.region  # 区域名称
        #             }
        #         }
        #     }
        # }
        response = requests.post(url, json=payload, headers=headers)
        print("返回码:",response.status_code)
        print("返回体:", response.text)
        if 201 == response.status_code:
            token = response.headers.get('X-Subject-Token', '')
            self.token = token
            return token
        else:
            # self.repeatRequestToken()
            # self.repeatCount = self.repeatCount + 1
            print("Get token failed!")
            # print(response.text)
            return None

    def get_token(self):
        """
        get the token for the ocr user from IAM server
        :return:
        """
        if self.token is not None:
            return self.token
        endpoint = 'iam.%s.myhuaweicloud.com' % self.region
        url = 'https://%s/v3/auth/tokens' % endpoint
        headers = {'Content-Type': 'application/json'}
        payload = {
          "auth": {
            "identity": {
              "methods": [
                "password"
              ],
              "password": {
                "user": {
                  "name": self.username,
                  "password": self.password,
                  "domain": {
                    "name": self.domainname
                  }
                }
              }
            },
            "scope": {
              "project": {
                "name": self.region  # 区域名称
              }
            }
          }
        }
        print(payload)
        response = requests.post(url, json=payload, headers=headers)
        if 201 == response.status_code:
            token = response.headers.get('X-Subject-Token', '')
            self.token = token
            return token
        else:
            # print(response);
            # self.repeatRequestToken()
            # self.repeatCount = self.repeatCount + 1
            print("Get token failed!")
            # print(response.text)
            return None


    def request_token(self):
        self.get_token()
        # self.token = None
        while True:
            if self.token is not None or self.repeatCount >= retry_times_max:
                # print(self.token);
                print("token request end")
                break
            else:

                time.sleep(delay_time)
                self.get_token()
                self.repeatCount += 1
                continue;






    def refresh_token(self):
        """
        refresh the attribute token
        :return:None
        """
        self.token=None
        self.get_token()

    def request_ocr_service_base64(self,uri,filepath,options=None):
        """
        :param uri: the uri for the http request to be called
        :param filepath: the fullpath of the file to be recognized
        :param options: optional parameter in the ocr http request
        :return:None
        """
        if filepath == "" or uri == "":
            raise ValueError("the parameter for request_ocr_service_base64 cannot be empty")
        self.request_token()
        # self.get_token_test()

        # print(self.token, self.repeatCount)
        url="https://"+self.httpendpoint+uri
        print(url)
        headers={
            'Content-Type':'application/json',
            'X-Auth-Token':self.token
        }
        # print(headers)
        with open(filepath,'rb') as bin_data:
            image_data=bin_data.read()
        image_base64= base64.b64encode(image_data).decode("utf-8")
        payload={}
        payload['image']=image_base64
        if options:
            payload.update(options)
        response=requests.post(url,json=payload,headers=headers)
        if 401 == response.status_code and ("Token expired" in response.text):
            #token expired,refresh token
            self.refresh_token()
            return self.request_ocr_service_base64(uri,filepath,options)

        print("request back success")
        return response




