#  Copyright (C) 2011  Statoil ASA, Norway. 
#   
#  The file 'fieldpanel.py' is part of ERT - Ensemble based Reservoir Tool. 
#   
#  ERT is free software: you can redistribute it and/or modify 
#  it under the terms of the GNU General Public License as published by 
#  the Free Software Foundation, either version 3 of the License, or 
#  (at your option) any later version. 
#   
#  ERT is distributed in the hope that it will be useful, but WITHOUT ANY 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or 
#  FITNESS FOR A PARTICULAR PURPOSE.   
#   
#  See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html> 
#  for more details. 


from PyQt4 import QtGui, QtCore
from ert_gui.widgets.combochoice import ComboChoice
from ert_gui.widgets.stringbox import DoubleBox
from ert_gui.widgets.pathchooser import PathChooser
from parametermodels import FieldModel
from ert.ert.enums import field_type
from ert_gui.widgets.helpedwidget import ContentModel

class FieldPanel(QtGui.QFrame):

    def __init__(self, parent):
        QtGui.QFrame.__init__(self, parent)

        self.setFrameShape(QtGui.QFrame.StyledPanel)
        self.setFrameShadow(QtGui.QFrame.Plain)
        self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)

        layout = QtGui.QFormLayout()
        layout.setLabelAlignment(QtCore.Qt.AlignRight)

        self.fieldModel = FieldModel("")

        self.fieldType = ComboChoice(self, field_type.values(), "", "config/ensemble/field_type")
        self.fieldType.setter = lambda model, value: self.typeChanged(field_type[str(value)])
        self.fieldType.getter = lambda model: str(self.fieldModel["type"])
        self.fieldType.initialize = ContentModel.emptyInitializer

        self.min = DoubleBox(self, "", "config/ensemble/field_min")
        self.modelWrap(self.min, "min")

        self.max = DoubleBox(self, "", "config/ensemble/field_max")
        self.modelWrap(self.max, "max")

        self.init = ComboChoice(self, ["None", "EXP", "LOG", "POW10", "ADD", "MUL", "RANDINT", "RANDFLOAT"], "", "config/ensemble/field_init")
        self.modelWrap(self.init, "init")

        self.output = ComboChoice(self, ["None", "EXP", "LOG", "POW10", "ADD", "MUL", "RANDINT", "RANDFLOAT"], "", "config/ensemble/field_output")
        self.modelWrap(self.output, "output")

        self.init_files = PathChooser(self, "", "config/ensemble/field_init_files", True)
        self.modelWrap(self.init_files, "init_files")

        self.file_generated_by_enkf = PathChooser(self, "", "config/ensemble/field_file_generated_by_enkf", True)
        self.modelWrap(self.file_generated_by_enkf, "enkf_outfile")

        self.file_loaded_by_enkf = PathChooser(self, "", "config/ensemble/field_file_loaded_by_enkf", True)
        self.modelWrap(self.file_loaded_by_enkf, "enkf_infile")

        self.min_std = PathChooser(self, "", "config/ensemble/field_min_std", True, must_be_set=False)
        self.modelWrap(self.min_std, "min_std")

        layout.addRow("Field type:", self.fieldType)
        layout.addRow("Include file:", self.file_generated_by_enkf) # generated by ERT
        layout.addRow("Init files:", self.init_files)
        layout.addRow("Min:", self.min)
        layout.addRow("Max:", self.max)
        layout.addRow("Init transformation:", self.init)
        layout.addRow("Output transformation:", self.output)
        layout.addRow("Min. std.:", self.min_std)
        layout.addRow("File loaded by EnKF:", self.file_loaded_by_enkf)

        self.setLayout(layout)

        self.typeChanged(field_type.ECLIPSE_RESTART)

    def modelWrap(self, widget, attribute):
        widget.initialize = ContentModel.emptyInitializer
        widget.setter = lambda model, value: self.fieldModel.set(attribute, value)
        widget.getter = lambda model: self.fieldModel[attribute]

    def typeChanged(self, value):
        setattr(self.fieldModel, "type", value)

        self.min.setEnabled(True)
        self.max.setEnabled(True)
        self.init.setEnabled(True)
        self.output.setEnabled(True)
        self.init_files.setEnabled(True)
        self.file_generated_by_enkf.setEnabled(True)
        self.file_loaded_by_enkf.setEnabled(True)
        self.min_std.setEnabled(True)

        if value == field_type.ECLIPSE_RESTART:
            self.init.setEnabled(False)
            self.output.setEnabled(False)
            self.init_files.setEnabled(False)
            self.file_generated_by_enkf.setEnabled(False)
            self.file_loaded_by_enkf.setEnabled(False)
            self.min_std.setEnabled(False)

        elif value == field_type.ECLIPSE_PARAMETER:
            self.file_loaded_by_enkf.setEnabled(False)

    def setFieldModel(self, fieldModel):
        self.fieldModel = fieldModel

        self.fieldType.fetchContent()
        self.min.fetchContent()
        self.max.fetchContent()
        self.init.fetchContent()
        self.output.fetchContent()
        self.init_files.fetchContent()
        self.file_generated_by_enkf.fetchContent()
        self.file_loaded_by_enkf.fetchContent()
        self.min_std.fetchContent()
