# emacquisition.py
#
#    Copyright (C) 2014 Diamond Light Source, Karl Levik
#
# 2014-09-24
#
# Methods to store EM acquisition data
#

import copy

from ispyb.sp.acquisition import Acquisition
from ispyb.strictordereddict import StrictOrderedDict


class EMAcquisition(Acquisition):
    """EMAcquisition provides methods to store data in the MotionCorrection and CTF tables."""

    def __init__(self):
        self.insert_data_collection_group = super(
            EMAcquisition, self
        ).upsert_data_collection_group
        self.insert_data_collection = super(EMAcquisition, self).upsert_data_collection
        self.update_data_collection_group = super(
            EMAcquisition, self
        ).upsert_data_collection_group
        self.update_data_collection = super(EMAcquisition, self).upsert_data_collection

    _movie_params = StrictOrderedDict(
        [
            ("movieId", None),
            ("dataCollectionId", None),
            ("movieNumber", None),
            ("movieFullPath", None),
            ("createdTimeStamp", None),
            ("positionX", None),
            ("positionY", None),
            ("nominalDefocus", None),
        ]
    )

    _motion_correction_params = StrictOrderedDict(
        [
            ("motionCorrectionId", None),
            ("movieId", None),
            ("autoProcProgramId", None),
            ("imageNumber", None),
            ("firstFrame", None),
            ("lastFrame", None),
            ("dosePerFrame", None),
            ("totalMotion", None),
            ("averageMotionPerFrame", None),
            ("driftPlotFullPath", None),
            ("micrographFullPath", None),
            ("micrographSnapshotFullPath", None),
            ("fftFullPath", None),
            ("fftCorrectedFullPath", None),
            ("patchesUsedX", None),
            ("patchesUsedY", None),
            ("comments", None),
        ]
    )

    _ctf_params = StrictOrderedDict(
        [
            ("ctfId", None),
            ("motionCorrectionId", None),
            ("autoProcProgramId", None),
            ("boxSizeX", None),
            ("boxSizeY", None),
            ("minResolution", None),
            ("maxResolution", None),
            ("minDefocus", None),
            ("maxDefocus", None),
            ("defocusStepSize", None),
            ("astigmatism", None),
            ("astigmatismAngle", None),
            ("estimatedResolution", None),
            ("estimatedDefocus", None),
            ("amplitudeContrast", None),
            ("ccValue", None),
            ("fftTheoreticalFullPath", None),
            ("comments", None),
        ]
    )

    _motion_correction_drift_params = StrictOrderedDict(
        [
            ("motionCorrectionDriftId", None),
            ("motionCorrectionId", None),
            ("frameNumber", None),
            ("deltaX", None),
            ("deltaY", None),
        ]
    )

    @classmethod
    def get_movie_params(cls):
        return copy.deepcopy(cls._movie_params)

    @classmethod
    def get_motion_correction_params(cls):
        return copy.deepcopy(cls._motion_correction_params)

    @classmethod
    def get_ctf_params(cls):
        return copy.deepcopy(cls._ctf_params)

    @classmethod
    def get_motion_correction_drift_params(cls):
        return copy.deepcopy(cls._motion_correction_drift_params)

    def insert_movie(self, values):
        """Store new movie params."""
        return self.get_connection().call_sp_write(procname="upsert_movie", args=values)

    def insert_motion_correction(self, values):
        """Store new motion correction params."""
        return self.get_connection().call_sp_write(
            procname="upsert_motion_correction", args=values
        )

    def insert_ctf(self, values):
        """Store new ctf params."""
        return self.get_connection().call_sp_write(procname="upsert_ctf", args=values)

    def insert_motion_correction_drift(self, values):
        """Store new motion correction drift params."""
        return self.get_connection().call_sp_write(
            procname="upsert_motion_correction_drift", args=values
        )

@@ -3117,101 +3117,8 @@
 s)%0A%0A
-    @classmethod%0A    def get_ctf_params(cls):%0A        return copy.deepcopy(cls._ctf_params)%0A%0A
     
@@ -3171,32 +3171,32 @@
 ft_params(cls):%0A
+
         return c
@@ -3655,53 +3655,596 @@
 ctf(
-self, values):%0A        %22%22%22Store new ctf
+%0A        self,%0A        ctf_id=None,%0A        motion_correction_id=None,%0A        auto_proc_program_id=None,%0A        boxSizeX=None,%0A        boxSizeY=None,%0A        minResolution=None,%0A        maxResolution=None,%0A        minDefocus=None,%0A        maxDefocus=None,%0A        defocusStepSize=None,%0A        astigmatism=None,%0A        astigmatismAngle=None,%0A        estimatedResolution=None,%0A        estimatedDefocus=None,%0A        amplitudeContrast=None,%0A        ccValue=None,%0A        fftTheoreticalFullPath=None,%0A        comments=None,%0A    ):%0A        %22%22%22Store new contrast transfer function
  param
+eter
 s.%22%22
@@ -4288,32 +4288,45 @@
 ).call_sp_write(
+%0A            
 procname=%22upsert
@@ -4331,28 +4331,622 @@
 rt_ctf%22,
- args=values
+%0A            args=%5B%0A                ctf_id,%0A                motion_correction_id,%0A                auto_proc_program_id,%0A                boxSizeX,%0A                boxSizeY,%0A                minResolution,%0A                maxResolution,%0A                minDefocus,%0A                maxDefocus,%0A                defocusStepSize,%0A                astigmatism,%0A                astigmatismAngle,%0A                estimatedResolution,%0A                estimatedDefocus,%0A                amplitudeContrast,%0A                ccValue,%0A                fftTheoreticalFullPath,%0A                comments,%0A            %5D,%0A        
 )%0A%0A    d

Rewrite EM insert_ctf() to accept parameters