#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Sends an alert to the static messenger of choice (defaulting to slack)
"""
from __future__ import annotations

import json
from collections import UserDict
from typing import List

import slack_sdk
from aws_xray_sdk.core import xray_recorder as xray

from iobot.slack import SlackHomePageView, SlackModalView
from iobot.models import IOBotList
from psi_logger import logger

@xray.capture()
class HomePageView(SlackHomePageView):
    @xray.capture()
    @logger.log
    def __init__(self,
                 log = None,
                 client: slack_sdk.WebClient = None,
                 title: str = "",
                 callback_id: str = "",
                 blocks: List[dict] = None):

        super().__init__(log, client, title=title, callback_id=callback_id, blocks=blocks)

    def __repr__(self) -> str:
        """Return a string representation of the object"""
        return f"{type(self)}"

@xray.capture()
class ModalView(SlackModalView):
    @xray.capture()
    @logger.log
    def __init__(self,
                 log,
                 client: slack_sdk.WebClient,
                 callback_id: str = "",
                 title: str = "",
                 close: str = "Close",
                 blocks: List[dict] = None,
                 metadata: dict = None,
                 request: dict = None):

        super().__init__(log, client, callback_id=callback_id, title=title, close=close, blocks=blocks, metadata=metadata, request=request)

    def __repr__(self) -> str:
        """Return a string representation of the object"""
        return f"{type(self)}"

@xray.capture()
class SearchView(ModalView):
    @logger.log
    def __init__(self,
                 log,
                 slack_client,
                 callback_id="IOBotSearchView",
                 title="Search",
                 close="Close",
                 blocks=None,
                 metadata=None,
                 action_id="",
                 block_id="",
                 search_info="",
                 search_placeholder="",
                 request: dict = None) -> None:

        super().__init__(log, slack_client, callback_id=callback_id,
                         title=title, close=close, blocks=blocks, metadata=metadata, request=request)

        self.add_blocks(
            IOBotList.to_slack_modal_search(action_id=action_id,
                                            block_id=block_id,
                                            search_info=search_info,
                                            search_placeholder=search_placeholder))


    def __repr__(self) -> str:
        """Return a string representation of the object"""
        return f"{type(self)}"


@xray.capture()
class SlackBlocks(UserDict):
    @xray.capture()
    @logger.log
    def __init__(self,
                 blocks: List[dict] = None):
        if blocks is None:
            blocks = []

        self.data['blocks'] = blocks

    def __delitem__(self, key):
        return self.data.pop(key)

    def __getitem__(self, key):
        return self.data.get(key)

    def __setitem__(self, key, value):
        if key in self:
            del self[key]
        self.data[key] = value

    def __to_dict__(self):
        return self.data.__dict__()

    @xray.capture()
    @logger.log
    def get(self, key):
        return self.data.get(key)

    @xray.capture()
    @logger.log
    def Blocks(self) -> list:
        return self.data['blocks']

    @xray.capture()
    @logger.log
    def add_block(self, block):
        self.data['blocks'].append(block)

    @xray.capture()
    @logger.log
    def add_blocks(self, blocks: list):
        for block in blocks:
            self.data['blocks'].append(block)

    def __repr__(self) -> str:
        """Return a string representation of the object"""
        return f"{type(self)}"