#!/usr/bin/env python
# -*- coding: utf-8 -*-
# import sys
import os
import unittest
from fbbotw import fbbotw


class FbbotwTest(unittest.TestCase):

    def setUp(self):
        self.fbid = os.getenv('FBID', '')
        self.postback_buttons = [
            {
                'type': 'postback',
                'title': 'Yes!',
                'payload': 'USER_SAY_YES'
            },
            {
                'type': 'postback',
                'title': 'Nope',
                'payload': 'USER_SAY_NOT'
            }
        ]
        self.quick_replies = [
            {
                'content_type': 'text',
                'title': 'Yes!',
                'payload': 'USER_SAY_YES'
            },
            {
                'content_type': 'text',
                'title': 'Nope',
                'payload': 'USER_SAY_NOT'
            }
        ]
        self.OK = 200
        self.RATE_LIMIT_CODE = 613

    #############################################
    #           User Profile API                #
    #############################################

    def test_get_user_info(self):
        response = fbbotw.get_user_information(fbid=self.fbid)
        # print(json.dumps(response, indent=4, sort_keys=True))
        self.assertTrue(isinstance(response['name'], str))
        self.assertTrue(len(response['name']) > 0)
        self.assertTrue(isinstance(response['first_name'], str))
        self.assertTrue(len(response['first_name']) > 0)
        self.assertTrue(isinstance(response['last_name'], str))
        self.assertTrue(len(response['last_name']) > 0)
        self.assertTrue(isinstance(response['profile_pic'], str))
        self.assertTrue(len(response['profile_pic']) > 0)

    #############################################
    #          Messenger Profile API            #
    #############################################

    def test_post_settings(self):
        greeting, button = fbbotw.post_settings("Hello world")
        greeting_code = greeting.status_code
        button_code = button.status_code
        if greeting_code == self.OK and button_code == self.OK:
            self.assertDictEqual(
                greeting.json(),
                {'result': 'Successfully updated greeting'}
            )
            self.assertDictEqual(
                button.json(),
                {'result': 'success'}
            )
        else:
            response_json = greeting.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )
            response_json = button.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )

    def test_post_greeting_text(self):
        greeting_texts = [
            {
                "locale": "default",
                "text": "Hello!"
            },
            {
                "locale": "pt_BR",
                "text": "Texto de Greeting em Português"
            },
            {
                "locale": "en_US",
                "text": "Greeting text in English USA"
            }
        ]
        response = fbbotw.post_greeting_text(
            greeting_texts=greeting_texts
        )
        if response.status_code == self.OK:
            self.assertEqual(response.status_code, self.OK)
            self.assertDictEqual(
                response.json(),
                {"result": "success"}
            )
        else:
            response_json = response.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )

    def test_post_start_button(self):
        response = fbbotw.post_start_button('GET_STARTED')
        if response.status_code == self.OK:
            self.assertEqual(response.status_code, self.OK)
            self.assertDictEqual(
                response.json(),
              
  {'result': 'success'}
            )
        else:
            response_json = response.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )

    def test_post_persistent_menu(self):
        persistent_menu = [
            {
                "composer_input_disabled": False,

                "locale": "default",
                "call_to_actions": [
                        {
                            "type": "nested",
                            "title": "First Option",
                            "call_to_actions": [
                                {
                                    "type": "postback",
                                    "title": "First Option of First Option",
                                    "payload": "YOUR_PAYLOAD"
                                },
                                {
                                    "type": "nested",
                                    "title": "Second Option of First Option",
                                    "call_to_actions": [
                                        {
                                            "type": "postback",
                                            "title": "ABC",
                                            "payload": "YOUR_PAYLOAD2"
                                        },
                                        {
                                            "type": "postback",
                                            "title": "CDE",
                                            "payload": "YOUR_PAYLOAD3"
                                        }
                                    ]
                                },
                            ]
                        },
                        {
                            "type": "postback",
                            "title": "Second Option",
                            "payload": "YOUR_PAYLOAD4"
                        },
                    ]
            }
        ]
        response = fbbotw.post_persistent_menu(persistent_menu)
        if response.status_code == self.OK:
            self.assertDictEqual(
                response.json(),
                {'result': 'success'}
            )
        else:
            response_json = response.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )

    def test_post_domain_whitelist(self):
        domain = ['https://breco.herokuapp.com']
        response = fbbotw.post_domain_whitelist(whitelisted_domains=domain)
        if response.status_code == self.OK:
            self.assertDictEqual(
                response.json(),
                {'result': 'success'}
            )
        else:
            response_json = response.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )

    def test_delete_domain_whitelist(self):
        response = fbbotw.delete_domain_whitelist()
        if response.status_code == self.OK:
            self.assertDictEqual(
                response.json(),
                {'result': 'success'}
            )
        else:
            response_json = response.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )

    def test_post_account_linking_url(self):
        url = 'https://breco.herokuapp.com'
        response = fbbotw.post_account_linking_url(account_linking_url=url)
        if response.status_code == self.OK:
            self.assertDictEqual(
                response.json(),
                {'result': 'success'}
            )
        else:
            response_json = response.json()
            self.assertEqual(
                response_json['error']['code'],
                self.RATE_LIMIT_CODE
            )

    def test_post_payment_settings(self):
        privacy_url = "https://breco.herokuapp.com/politica-e-termos"
        response = fbbotw.post_payment_settings(privacy_url=privacy_url)
        if response.status_code == self.OK:
            self.assertDictEqual(
                response.json(),
                {