#------------------------------------------------
#   KZライブラリ    ガイド押出
#                   Ver 3.04.07
#                   written by Fukurou
#------------------------------------------------
import bpy
import gpu

from bpy.props import *
from mathutils import Vector
from mathutils import Matrix
from gpu_extras.batch import batch_for_shader
from .module import kzf_mod_base
from .module import kzf_mod_guide
from . import kzf_std_base
#from . kzf_std_mouse import KZWORKSHOP_OT_StdMouse
#from . kzf_cls_fremouse import KZWORKSHOP_OT_ClsFreMouse

#-----------------------------------------------------------
# ダイアログインプット
#-----------------------------------------------------------
class KZWORKSHOP_OT_EdtDlgGpush(bpy.types.Operator):
    bl_idname = "kzf_edt.dlggpush"
    bl_label  = "転写"
    bl_options = {'REGISTER', 'UNDO'}

    sel_objname = []
    #座標データ
    face = 0
    pos_path = []    
    #プロパティ
    offset: FloatVectorProperty(
        name = "オフセット",
        description="",
        default = (0,0,0),
        step = 1,
        precision=3,
    )
    lines: BoolProperty(
        name = "連結線あり",
        description="連結線表示",
        default = True,
    )
    # 実行可能か
    @classmethod
    def poll(cls, context):
        # オブジェクトが選択されているときのみメニューを表示させる
        for o in bpy.data.objects:
            if o.select_get():
                return True
        return False

    #実行
    def execute(self, context):
        #アクティブレイヤー設定
        #kzf_mod_base.setactive_clayer('F_Section')
        move_main(self, context)
        return {'FINISHED'}

    #ダイアログ呼出
    def invoke(self, context, event):
        self.sel_objname.clear()
        #プロパティ
        for object in bpy.context.selected_objects:
            self.sel_objname.append(object.name)
        props      = context.scene.kzproperty
        self.sec   = props.kf_section
        self.gname = props.kf_gname
        wm = context.window_manager
        return wm.invoke_props_dialog(self)
        
    def draw(self, context):
        layout = self.layout
        box = layout.box()
        col = box.column()
        col.use_property_split = True   #メニューを分割表示
        col.label(text="オプション")
        col.prop(self, "offset")
        col.prop(self, "lines")

#-----------------------------------------------------------
# 移動・コピー
#-----------------------------------------------------------
def move_main(self, context):
    offset = Vector(self.offset)
    #書込みコレクション
    gname = kzf_mod_guide.to_collection('F')
    #オブジェクトのリストを取得する
    sel_objects = []
    for name in self.sel_objname:
        object = bpy.data.objects[name]
        sel_objects.append(object)
    # オブジェクトを複製する
    for object in sel_objects:
        copy_object(self.sec, gname, object, offset)
    if not self.lines:
        return
    #連結線
    list = kzf_mod_guide.get_outline(sel_objects)
    for point in list:
        end = point + offset
        verts = [point, end]
        kzf_mod_guide.make_line(gname, "Line", verts)
    # 選択解除
    #bpy.ops.object.select_all(action='DESELECT')

#-----------------------------------------------------------
# オブジェクトコピー
#-----------------------------------------------------------
def copy_object(sec, gname, object, offset):
    code = object.get("obj_code")
    #print("OBJECT=", object, code)
    voffs = Vector((offset))
    if code == 2:
        p1, p2 = kzf_mod_guide.get_topend(object)
        top = p1 + voffs
        end = p2 + voffs
        verts = [top, end]
        kzf_mod_guide.make_line(gname, "Line", verts)
    if code == 4:
        # カスタムプロパティから辞書を取得
        arc_data_dict = object.get("arc_data")
        center   = arc_data_dict["center"]
        radius   = arc_data_dict["radius"]
        st       = arc_data_dict["start_angle"]
        ed       = arc_data_dict["end_angle"]
        segments = arc_data_dict["segments"]
        normal   = arc_data_dict["normal"]
        ctype    = arc_data_dict["ctype"]
        cpnt     = Vector((center)) + voffs
        #書込みコレクション
        #
        kzf_mod_guide.make_arc(gname, "Arc(C)", sec, cpnt, radius, st, ed, normal, segments, ctype)

