# ===============================================================================
# Copyright 2014 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= enthought library imports =======================
from __future__ import absolute_import

import os

from envisage.ui.tasks.task_extension import TaskExtension
from envisage.ui.tasks.task_factory import TaskFactory
from pyface.tasks.action.schema import SMenu, SGroup
from pyface.tasks.action.schema_addition import SchemaAddition
from traits.api import Any

from pychron.core.helpers.filetools import glob_list_directory
from pychron.envisage.tasks.base_task_plugin import BaseTaskPlugin
from pychron.envisage.tasks.list_actions import SpectrometerScriptAction, HopsAction
from pychron.paths import paths
from pychron.spectrometer.base_spectrometer_manager import BaseSpectrometerManager
from pychron.spectrometer.ion_optics.ion_optics_manager import IonOpticsManager
from pychron.spectrometer.readout_view import ReadoutView
from pychron.spectrometer.scan_manager import ScanManager
from pychron.spectrometer.tasks.spectrometer_actions import (
    PopulateMFTableAction,
    SendConfigAction,
    ViewReadoutAction,
    EditGainsAction,
    ReloadMFTableAction,
    MagnetFieldTableAction,
    MagnetFieldTableHistoryAction,
    ToggleSpectrometerTask,
    PeakCenterAction,
    DefinePeakCenterAction,
    CoincidenceScanAction,
    SpectrometerParametersAction,
)
from pychron.spectrometer.tasks.spectrometer_preferences import (
    SpectrometerPreferencesPane,
)
from pychron.spectrometer.tasks.spectrometer_task import SpectrometerTask


class BaseSpectrometerPlugin(BaseTaskPlugin):
    spectrometer_manager = Any
    spectrometer_manager_klass = None
    task_klass = SpectrometerTask
    manager_name = ""
    scan_manager = Any
    ion_optics_manager = Any

    def start(self):
        super(BaseSpectrometerPlugin, self).start()
        if self.spectrometer_manager:
            self.spectrometer_manager.spectrometer.start()

    # ===============================================================================
    # tests
    # ===============================================================================
    def test_communication(self):
        manager = self.spectrometer_manager
        return manager.test_connection()

    def test_intensity(self):
        manager = self.spectrometer_manager
        ret = manager.test_connection(force=False)
        if ret and ret[0]:
            ret = manager.test_intensity()

        return ret

    # def _inspector_task_factory(self):
    #     from pychron.spectrometer.tasks.inspector.scan_inspector_task import ScanInspectorTask
    #
    #     t = ScanInspectorTask()
    #     return t

    def _mass_cal_task_factory(self):
        from pychron.spectrometer.tasks.mass_cal.mass_calibration_task import (
            MassCalibrationTask,
        )

        t = MassCalibrationTask(spectrometer_manager=self.spectrometer_manager)
        return t

    def _task_factory(self):
        t = self.task_klass(
            manager=self.spectrometer_manager,
            scan_manager=self.scan_manager,
            application=self.application,
        )
        return t

    def _factory_spectrometer(self):
        return self.spectrometer_manager

    def _factory_ion_optics(self):
        return self.ion_optics_manager

    def _factory_scan_manager(self):
        return self.scan_manager

    def _tasks_default(self):
        ts = [
            TaskFactory(
                id="pychron.spectrometer",
                task_group="hardware",
                factory=self._task_factory,
                accelerator="Ctrl+'",
                name="Spectrometer",
                image="spectrum_emission",
            ),
            TaskFactory(
                id="pychron.mass_calibration",
                factory=self._mass_cal_task_factory,
                name="Mass Calibration",