# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

__authors__ = ["C. Nemoz", "H. Payno"]
__license__ = "MIT"
__date__ = "25/10/2016"

import logging
import os

from Orange.widgets import widget
from Orange.widgets.settings import Setting
from silx.gui import qt
from orangecontrib.tomwer.widgets.utils import (convertInputsForOrange,
                                                convertOutputsForOrange)
from tomwer.core.ReconsParams import ReconsParam
from tomwer.core.ftseries.FtserieReconstruction import FtserieReconstruction
from tomwer.web.client import OWClient
from tomwer.core.ftseries.Ftseries import Ftseries, logger as FTSLogger
from tomwer.gui.FtseriesMixIn import _FtseriesMixIn

logger = logging.getLogger(__name__)


class FtseriesWidget(_FtseriesMixIn, widget.OWWidget, OWClient):
    name = "ftseries reconstruction"
    id = "orange.widgets.tomwer.ftseries"
    description = "This widget manage the reconstruction of acquisitions"
    icon = "icons/bricks.png"
    priority = 13
    category = "esrfWidgets"
    keywords = ["tomography", "file", "tomwer"]

    inputs = convertInputsForOrange(Ftseries.inputs)
    outputs = convertOutputsForOrange(Ftseries.outputs)

    want_main_area = True
    resizing_enabled = True
    allows_cycle = True
    compress_signal = False

    _rpSetting = Setting(ReconsParam().params)

    def __init__(self, parent=None):
        """Widget which read the .hdf5 generated by octave and modify it.
        Then run a subprocess to call octave and run ftseries"""
        OWClient.__init__(self, (logger, FTSLogger))
        widget.OWWidget.__init__(self, parent)
        _FtseriesMixIn.__init__(self)
        self.reconsParams.setStructs(self._rpSetting)

    def _signalReconsReady(self, scanID):
        scan = scanID
        if type(scanID) is FtserieReconstruction:
            scan = scanID.scanID
        _FtseriesMixIn._signalReconsReady(self, scanID)
        # notice that action on the scan has been done
        self.send("data", scan)


def main():
    import sys
    import shutil
    app = qt.QApplication([])
    s = FtseriesWidget()

    if len(sys.argv) == 2:
        folder = sys.argv[1]
    else:
        import tempfile
        folder = tempfile.mkdtemp()

    s.updatePath(folder)
    s.show()
    app.exec_()
    if os.path.isdir(folder):
        shutil.rmtree(folder)


if __name__ == "__main__":
    main()
