# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'chat_dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

from common.vars import SENDER,MESSAGE_TEXT
from PyQt5 import QtCore, QtWidgets


# Класс обработки окна диалога пользователь - пользователь
class Chat_Dialog(QtWidgets.QDialog):
    '''
    Класс обработки окна диалога пользователь - пользователь
    '''

    def __init__(self, user_nickname, sender, reader):
        super().__init__()
        self.ui = Ui_Chat_windows()
        self.ui.setupUi(self)
        self.user_nickname = user_nickname
        self.sender = sender
        self.reader = reader
        self.ui.contact_nicktame.setText(user_nickname)
        self.ui.pushButton.clicked.connect(self.send_message)
        self.reader.message_from_user.connect(self.get_message)
    # Обработка сообщения от пользователя
    def get_message(self, message):
        '''
        Обработка сообщения от пользователя
        '''
        print('---  Message come ---- ')
        if message[SENDER] == self.user_nickname and message[MESSAGE_TEXT]:
            message_user = self.user_nickname + ' : ' + message[MESSAGE_TEXT]
            self.ui.listWidget.addItem(message_user)
        print(message)

    # Отправка сообщения пользователю
    def send_message(self):
        '''
        Отправка сообщения пользователю
        '''
        message = self.ui.lineEdit.text()
        print('Account name', self.user_nickname)
        print(message)
        self.sender.create_message(to=self.user_nickname, message=message)
        message = self.sender.account + ' : ' + message
        print(message)
        self.ui.listWidget.addItem(message)
        self.ui.lineEdit.clear()




# Форма диалог польователь - пользователь
class Ui_Chat_windows(object):
    '''
    Форма диалог польователь - пользователь
    '''
    def setupUi(self, Chat_windows):
        Chat_windows.setObjectName("Chat_windows")
        Chat_windows.resize(400, 300)
        self.label = QtWidgets.QLabel(Chat_windows)
        self.label.setGeometry(QtCore.QRect(20, 10, 41, 16))
        self.label.setObjectName("label")
        self.contact_nicktame = QtWidgets.QLabel(Chat_windows)
        self.contact_nicktame.setEnabled(True)
        self.contact_nicktame.setGeometry(QtCore.QRect(70, 10, 58, 16))
        self.contact_nicktame.setText("")
        self.contact_nicktame.setObjectName("contact_nicktame")
        self.pushButton = QtWidgets.QPushButton(Chat_windows)
        self.pushButton.setGeometry(QtCore.QRect(280, 240, 112, 32))
        self.pushButton.setObjectName("pushButton")
        self.listWidget = QtWidgets.QListWidget(Chat_windows)
        self.listWidget.setGeometry(QtCore.QRect(20, 30, 361, 192))
        self.listWidget.setObjectName("listWidget")
        self.lineEdit = QtWidgets.QLineEdit(Chat_windows)
        self.lineEdit.setGeometry(QtCore.QRect(20, 240, 241, 21))
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Chat_windows)
        QtCore.QMetaObject.connectSlotsByName(Chat_windows)

    def retranslateUi(self, Chat_windows):
        _translate = QtCore.QCoreApplication.translate
        Chat_windows.setWindowTitle(_translate("Chat_windows", "Chat windows"))
        self.label.setText(_translate("Chat_windows", "Чат с "))
        self.pushButton.setText(_translate("Chat_windows", "Отправить"))
