#
# Copyright (C) 2016-2019 by Nathan Lovato, Daniel Oakey, Razvan Radulescu, and contributors
#
# This file is part of Power Sequencer.
#
# Power Sequencer 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.
#
# Power Sequencer 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 for more details.
#
# You should have received a copy of the GNU General Public License along with Power Sequencer. If
# not, see <https://www.gnu.org/licenses/>.
#
import bpy
from math import ceil

from .utils.global_settings import SequenceTypes
from .utils.functions import slice_selection
from .utils.functions import find_linked
from .utils.doc import doc_name, doc_idname, doc_brief, doc_description


class POWER_SEQUENCER_OT_speed_up_movie_strip(bpy.types.Operator):
    """
    *brief* Adds a speed effect to the  2x speed, set frame end, wrap both into META

    Add 2x speed to strip and set its frame end accordingly. Wraps both the strip and the speed
    modifier into a META strip.
    """

    doc = {
        "name": doc_name(__qualname__),
        "demo": "https://i.imgur.com/ZyEd0jD.gif",
        "description": doc_description(__doc__),
        "shortcuts": [
            (
                {"type": "NUMPAD_2", "value": "PRESS", "alt": True},
                {"speed_factor": 2.0},
                "Speed x2",
            ),
            (
                {"type": "NUMPAD_3", "value": "PRESS", "alt": True},
                {"speed_factor": 3.0},
                "Speed x3",
            ),
            (
                {"type": "NUMPAD_4", "value": "PRESS", "alt": True},
                {"speed_factor": 4.0},
                "Speed x4",
            ),
        ],
        "keymap": "Sequencer",
    }
    bl_idname = doc_idname(__qualname__)
    bl_label = doc["name"]
    bl_description = doc_brief(doc["description"])
    bl_options = {"REGISTER", "UNDO"}

    speed_factor: bpy.props.IntProperty(
        name="Speed factor", description="How many times the footage gets sped up", default=2, min=0
    )
    individual_sequences: bpy.props.BoolProperty(
        name="Affect individual strips",
        description="Speed up every VIDEO strip individually",
        default=False,
    )

    @classmethod
    def poll(cls, context):
        return context.selected_sequences

    def execute(self, context):
        sequences = [s for s in context.selected_sequences if s.type in SequenceTypes.VIDEO]

        if not sequences:
            self.report(
                {"ERROR_INVALID_INPUT"},
                "No Movie meta_strip or Metastrips selected. Operation cancelled",
            )
            return {"FINISHED"}

        selection_blocks = []
        if self.individual_sequences:
            selection_blocks = [[s] for s in sequences]
        else:
            selection_blocks = slice_selection(context, sequences)

        for sequences in selection_blocks:
            self.speed_effect_add(context, sequences)

        self.report(
            {"INFO"}, "Successfully processed " + str(len(selection_blocks)) + " selection blocks"
        )
        return {"FINISHED"}

    def speed_effect_add(self, context, sequences):
        if not sequences:
            return

        sequence_editor = context.scene.sequence_editor
        bpy.ops.sequencer.select_all(action="DESELECT")
        for s in sequences:
            s.select = True
        bpy.ops.sequencer.meta_make()
        meta_strip = sequence_editor.active_strip
        if len(meta_strip.sequences) == 1:
            meta_strip.sequences[0].frame_offset_start = 0
            meta_strip.sequences[0].frame_offset_end = 0

        bpy.ops.sequencer.effect_strip_add(type="SPEED")
        speed_effect = sequence_editor.active_strip
        speed_effect.use_default_fade = False
        speed_effect.speed_factor = self.speed_factor

        duration = ceil(meta_strip.frame_final_duration / speed_effect.speed_factor)
        meta_strip.frame_final_end = meta_strip.frame_final_start + duration

        sequence_editor.active_strip = meta_strip
        speed_effect.select = True
        meta_strip.select = True
        bpy.ops.sequencer.meta_make()
        sequence_editor.active_strip.name = (
            meta_strip.sequences[0].name + " " + str(self.speed_factor) + "x"
        )
