# Path: src/gui/line_edit_color_validator.py
# class LineEditColorValidator(object):
# 
#     def __init__(self, line_edit, re_mask, error_color='#f6989d', error_tooltip=''):
#         self.error_color = error_color
#         self.error_msg = error_tooltip
#         self.line_edit = line_edit
#         self.line_edit.textChanged.connect(self.on_changed)
#         self.re_mask = re_mask
#         self.re_comp = re.compile(self.re_mask)
#         self.on_changed()  # init
# 
#     def is_valid(self):
#         text = self.line_edit.text()
#         return self.re_comp.match(text)
# 
#     def on_changed(self, text=''):
#         if self.is_valid():
#             self.set_normal_style()
#         else:
#             self.set_error_style()
# 
#     def set_normal_style(self):
#         self.line_edit.setStyleSheet('')
#         self.line_edit.setToolTip('')
# 
#     def set_error_style(self):
#         self.line_edit.setStyleSheet('QLineEdit { background-color: %s }' % self.error_color)
#         self.line_edit.setToolTip(self.error_msg)


import os
from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QWidget, QMessageBox
from .line_edit_color_validator import LineEditColorValidator


class EditorWidgetWfs(QWidget, FORM_CLASS):

    def __init__(self, parent=None):
        super(EditorWidgetWfs, self).__init__(parent)
        self.setupUi(self)
        self.wfs_validator = LineEditColorValidator(self.txtUrl, 'http[s]?://.+', error_tooltip='http{s}://any_text')
        # self.txtUrl.textChanged.connect(self.set_layers_names)

    def feel_form(self, ds_info):
        self.ds_info = ds_info
        self.txtUrl.setText(ds_info.wfs_url)
        self.txtParams.setText(ds_info.wfs_params)
        self.txtLayers.setText(",".join(ds_info.wfs_layers))
        self.chkTurnOver.setChecked(ds_info.wfs_turn_over if ds_info.wfs_turn_over else False)

    def feel_ds_info(self, ds_info):
        ds_info.wfs_url = self.txtUrl.text()
        ds_info.wfs_params = self.txtParams.text()
        ds_info.wfs_layers = self.txtLayers.text().split()
        ds_info.wfs_turn_over = self.chkTurnOver.isChecked()

    def validate(self, ds_info):
        if not ds_info.wfs_url:
            QMessageBox.critical(self, self.tr('Error on save data source'), self.tr('Please, enter WFS url'))
            return False

        if not self.wfs_validator.is_valid():
                QMessageBox.critical(self, self.tr('Error on save data source'), self.tr('Please, enter correct value for WMS url'))

# Path: src/gui/line_edit_color_validator.py
# class LineEditColorValidator(object):
# 
#     def __init__(self, line_edit, re_mask, error_color='#f6989d', error_tooltip=''):
#         self.error_color = error_color
#         self.error_msg = error_tooltip
#         self.line_edit = line_edit
#         self.line_edit.textChanged.connect(self.on_changed)
#         self.re_mask = re_mask
#         self.re_comp = re.compile(self.re_mask)
#         self.on_changed()  # init
# 
#     def is_valid(self):
#         text = self.line_edit.text()
#         return self.re_comp.match(text)
# 
#     def on_changed(self, text=''):
#         if self.is_valid():
#             self.set_normal_style()
#         else:
#             self.set_error_style()
# 
#     def set_normal_style(self):
#         self.line_edit.setStyleSheet('')
#         self.line_edit.setToolTip('')
# 
#     def set_error_style(self):
#         self.line_edit.setStyleSheet('QLineEdit { background-color: %s }' % self.error_color)
#         self.line_edit.setToolTip(self.error_msg)


# Path: src/gui/editor_widget_wfs.py
import os
from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QWidget, QMessageBox
from .line_edit_color_validator import LineEditColorValidator



class EditorWidgetWfs(QWidget, FORM_CLASS):

    def __init__(self, parent=None):
        super(EditorWidgetWfs, self).__init__(parent)
        self.setupUi(self)
        self.wfs_validator = LineEditColorValidator(self.txtUrl, 'http[s]?://.+', error_tooltip='http{s}://any_text')
        # self.txtUrl.textChanged.connect(self.set_layers_names)

    def feel_form(self, ds_info):
        self.ds_info = ds_info
        self.txtUrl.setText(ds_info.wfs_url)
        self.txtParams.setText(ds_info.wfs_params)
        self.txtLayers.setText(",".join(ds_info.wfs_layers))
        self.chkTurnOver.setChecked(ds_info.wfs_turn_over if ds_info.wfs_turn_over else False)

    def feel_ds_info(self, ds_info):
        ds_info.wfs_url = self.txtUrl.text()
        ds_info.wfs_params = self.txtParams.text()
        ds_info.wfs_layers = self.txtLayers.text().split()
        ds_info.wfs_turn_over = self.chkTurnOver.isChecked()

    def validate(self, ds_info):
        if not ds_info.wfs_url:
            QMessageBox.critical(self, self.tr('Error on save data source'), self.tr('Please, enter WFS url'))
            return False

        if not self.wfs_validator.is_valid():
                QMessageBox.critical(self, self.tr('Error on save data source'), self.tr('Please, enter correct value for WMS url'))

                return False