# Path: operators/tools/get_text_strips.py
# def get_text_strips(scene, channel=None):
#     """Get all the text strips in the edit channel"""
# 
#     if channel == None:
#         channel = scene.subtitle_edit_channel
# 
#     all_strips = list(sorted(scene.sequence_editor.sequences,
#             key=lambda x: x.frame_start))
# 
#     text_strips = []
#     for strip in all_strips:
#         if (strip.channel == channel and
#                 strip.type == 'TEXT'):
#             text_strips.append(strip)
# 
#     return text_strips
# 
# Path: operators/tools/get_font.py
# def get_font(font_path):
# 
# 	if os.path.isfile(font_path):
# 		return bpy.data.fonts.load(font_path, check_existing=True)
# 	else:
# 		return None


import bpy
from .tools.get_text_strips import get_text_strips
from .tools.get_font import get_font



class SEQUENCER_OT_refresh_font_data(bpy.types.Operator):
    bl_label = "Refresh Font Data"
    bl_idname = 'sequencerextra.refresh_font_data'
    bl_description = "Refresh the font size for all text strips on the edit channel"

    @classmethod
    def poll(self, context):
        scene = context.scene
        try:
            text_strips = get_text_strips(scene)

            if len(text_strips) > 0:
                return True
            else:
                return False
        except AttributeError:
            return False

    def execute(self, context):

        scene = context.scene

        text_strips = get_text_strips(scene)

        for strip in text_strips:
            strip.font_size = scene.subtitle_font_size
            strip.font = get_font(scene.subtitle_font)

# Path: operators/tools/get_text_strips.py
# def get_text_strips(scene, channel=None):
#     """Get all the text strips in the edit channel"""
# 
#     if channel == None:
#         channel = scene.subtitle_edit_channel
# 
#     all_strips = list(sorted(scene.sequence_editor.sequences,
#             key=lambda x: x.frame_start))
# 
#     text_strips = []
#     for strip in all_strips:
#         if (strip.channel == channel and
#                 strip.type == 'TEXT'):
#             text_strips.append(strip)
# 
#     return text_strips
# 
# Path: operators/tools/get_font.py
# def get_font(font_path):
# 
# 	if os.path.isfile(font_path):
# 		return bpy.data.fonts.load(font_path, check_existing=True)
# 	else:
# 		return None


# Path: operators/refresh_font_data.py
import bpy
from .tools.get_text_strips import get_text_strips
from .tools.get_font import get_font




class SEQUENCER_OT_refresh_font_data(bpy.types.Operator):
    bl_label = "Refresh Font Data"
    bl_idname = 'sequencerextra.refresh_font_data'
    bl_description = "Refresh the font size for all text strips on the edit channel"

    @classmethod
    def poll(self, context):
        scene = context.scene
        try:
            text_strips = get_text_strips(scene)

            if len(text_strips) > 0:
                return True
            else:
                return False
        except AttributeError:
            return False

    def execute(self, context):

        scene = context.scene

        text_strips = get_text_strips(scene)

        for strip in text_strips:
            strip.font_size = scene.subtitle_font_size
            strip.font = get_font(scene.subtitle_font)

            strip.location[1] = scene.subtitle_font_height