import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.2
import EncryptorSettings 1.0

Item {
    property var viewModel: encryptorSettings

    EncryptorSettings {
        id: encryptorSettings
    }

    Column {
        id: settingsColumn
        anchors.fill: parent

        Item {
            height: 10
            width: parent.width
        }

        Button {
            width: parent.width
            text: qsTr("Обновить список портов")

            onClicked: {
                enabled = false;
                viewModel.update();
                enabled = true;
            }
        }

        Item {
            height: 10
            width: parent.width
        }

        Label {
            width: parent.width
            text: qsTr("Порт модема")
        }

        ComboBox {
            id: deviceSerialPort
            width: parent.width
            model: viewModel.outputPortList

            onCurrentTextChanged: {
                viewModel.outputPort = currentText;
            }

            Component.onCompleted: {
                viewModel.outputPort = currentText;
            }
        }

        Item {
            height: 10
            width: parent.width
            visible: true
        }

        Loader {
            width: parent.width
            sourceComponent: Qt.platform.os === "linux" ? unixExternalPort
                                                        : winInternalPort
        }

        Item {
            height: 10
            width: parent.width
        }

        Button {
            width: parent.width
            visible: true
            enabled: (viewModel.inputPort !== "" &&
                      viewModel.outputPort !== "" &&
                      !encryptorManager.encryptionEnabled)

            text: qsTr("Запустить шифратор");

            onClicked: {
                encryptorManager.initialize(viewModel.internalPort,
                                            viewModel.externalPort,
                                            viewModel.outputPort)
            }
        }

        Button {
            width: parent.width
            visible: true
            enabled: encryptorManager.encryptionEnabled
            text: qsTr("Остановить шифратор")

            onClicked: {
                encryptorManager.deinitialize();
            }
        }
    }

    Component {
        id: unixExternalPort

        Column {
            Label {
                width: parent.width
                text: qsTr("Внешний COM-порт")
                visible: true
            }

            TextField {
                id: softwareSerialPort
                readOnly: true
                text: viewModel.externalPort
                visible: true
                width: parent.width
                selectByMouse: true
            }
        }
    }

    Component {
        id: winInternalPort

        Column {
            Label {
                width: parent.width
                text: qsTr("Виртуальный COM-порт")
                visible: true
            }

            ComboBox {
                id: internalSerialPort
                width: parent.width
                model: viewModel.outputPortList

                onCurrentTextChanged: {
                    viewModel.internalPort = currentText;
                }

                Component.onCompleted: {
                    viewModel.internalPort = currentText;
                }
            }
        }
    }
}
