import requests

from . import config


class wordpress:

    @staticmethod
    def get_post(turkdl_id):
        data = {'turkdl_id': turkdl_id}
        headers = {'token': config.token}
        response = requests.get(config.get_post_url, json=data, headers=headers)

        if response.status_code == 200:
            return {
                'status': True,
                'message': response.json()
            }

        return {
            'status': False,
            'message': response.text
        }

    @staticmethod
    def create_post(data):
        headers = {'token': config.token}
        response = requests.post(config.create_post_url, json=data, headers=headers)

        try:
            response.json()

        except Exception as e:
            print('create_post error')
            print(response.text)
            print(e)
            return {
                'status': False,
                'message': ''
            }

        if response.status_code == 200:
            return {
                'status': True,
                'message': response.json()
            }

        return {
            'status': False,
            'message': response.json()
        }

    @staticmethod
    def update_post(post_id, data):
        params = {
            'post_id': post_id,
            'data': data,
        }
        headers = {'token': config.token}
        response = requests.post(config.update_post_url, json=params, headers=headers)

        if response.status_code == 200:
            return {
                'status': True,
                'message': response.json()
            }

        return {
            'status': False,
            'message': response.json()
        }

    @staticmethod
    def update_create_post(data):
        headers = {'token': config.token}
        response = requests.post(config.update_create_post_url, json=data, headers=headers)
        
        # print("Sending:", config.update_create_post_url)
        # print("Headers:", headers)
        # print("Data:", data)

        try:
            response.json()

        except Exception as e:
            print('create_post error')
            print(response.text)
            print(e)
            return {
                'status': False,
                'message': ''
            }

        if response.status_code == 200:
            return {
                'status': True,
                'message': response.json()
            }

        return {
            'status': False,
            'message': response.json()
        }