import UIKit
import TigaseSwift

// FIXME: solve linter suppressed issues
// swiftlint: disable force_unwrapping
// swiftlint: disable force_cast
class InvitationChatTableViewCell: BaseChatTableViewCell {
    @IBOutlet var messageField: UILabel!
    @IBOutlet var acceptButton: UIButton!

    @IBOutlet var defBottomButtonConstraint: NSLayoutConstraint?

    private var account: BareJID?
    private var appendix: ChatInvitationAppendix?

    private var buttonBottomContraint: NSLayoutConstraint?

    func set(invitation: ChatInvitation, indexPath: IndexPath) {
        super.set(item: invitation, indexPath: indexPath)
        self.account = invitation.account
        self.appendix = invitation.appendix
        acceptButton.layer.borderWidth = 2.0
        acceptButton.layer.borderColor = UIColor(named: "tintColor")?.cgColor
        acceptButton.layer.cornerRadius = acceptButton.frame.height / 2
        if invitation.state.direction == .incoming, let account = self.account, let channel = self.appendix?.channel {
            acceptButton.isHidden = DBChatStore.instance.getChat(for: account, with: channel) != nil
        } else {
            acceptButton.isHidden = true
        }
        if acceptButton.isHidden {
            if buttonBottomContraint == nil {
                buttonBottomContraint = self.stateView!.bottomAnchor.constraint(equalTo: self.messageField.bottomAnchor)
            }
            buttonBottomContraint?.priority = .required
            defBottomButtonConstraint?.isActive = false
            buttonBottomContraint?.isActive = true
        } else {
            buttonBottomContraint?.isActive = false
            defBottomButtonConstraint?.isActive = true
        }
        self.messageField.text = invitation.message ?? "Invitation to channel \(invitation.appendix.channel.stringValue)"
    }

    var viewController: UIViewController? {
        var parent: UIResponder? = self
        while parent != nil {
            parent = parent?.next
            if let controller = parent as? UIViewController {
                return controller
            }
        }
        return nil
    }

    @IBAction func acceptClicked(_ sender: UIButton) {
        guard let account = self.account, let mixInvitation = appendix?.mixInvitation() else {
            return
        }

        let controller = UIStoryboard(name: "MIX", bundle: nil).instantiateViewController(withIdentifier: "ChannelJoinViewController") as! ChannelJoinViewController
        controller.account = account
        controller.componentType = .mix
        controller.mixInvitation = mixInvitation

        controller.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: controller, action: #selector(ChannelJoinViewController.cancelClicked(_:)))

        let navitation = UINavigationController(rootViewController: controller)
        navitation.modalPresentationStyle = .formSheet

        viewController?.present(navitation, animated: true, completion: nil)
    }
}
