import requests
from utils.logger import logger
import json


class PostAllSenFillAnswers(object):
    def __init__(self, common, headers, accesstoken):
        self.headers = headers
        self.baseUrl = common.get('baseUrl')
        self.headers.update({'Content-Length': '90'})
        self.accesstoken = accesstoken
        self.headers.update({"accesstoken": self.accesstoken})
        self.headers.update({"Host": common.get("baseProxy")})

    def post_all_sen_fill_answer(self, taskID, user_answer):
        url = "{}/userListening/{}/senFillAnswers".format(self.baseUrl, taskID)
        data = user_answer
        response = requests.request("POST", url, headers=self.headers, json=(data))
        answer = response.text
        # if answer.get("message") == "success":
        #     logger.info("[PASS] == {},{}", taskID, user_answer)
        # if answer.get("message") != "success":
        #     logger.info("[FAIL] == {},{}", taskID, user_answer)

    def put_sen_fill_words(self, data):
        url = "{}/vocabularies/familiarity".format(self.baseUrl)
        self.headers.update({"Content-Type": "application/json; charset=utf-8"})
        response = requests.request("PUT", url, headers=self.headers, json=data)
        # print("R", response)
        return response

    def put_sen_fill_done(self, data, taskID):
        url = "{}/userVocabulary/{}/vocStatus".format(self.baseUrl, taskID)
        res = requests.request("PUT", url, headers=self.headers, json=data)
        return res