from PyQt5.QtWidgets import QAction, QTreeWidget, QTreeWidgetItem, QFileDialog
from PyQt5.QtGui import  QIcon
from PyQt5.QtCore import Qt
import animations.general_animation as j3d
from widgets.yaz0 import compress, compress_slow, compress_fast
from io import BytesIO

class tree_item(QTreeWidgetItem):
    def __init__(self, parent):
        QTreeWidgetItem.__init__(self, parent,1000)
        self.display_info = []
        self.filepath = ""
        self.compressed = 1
        self.bmd_file = None
        self.sound_data = None
        
        self.changed = False
        
    def set_values(self, display_info, filepath, compressed ):
        self.display_info = display_info
        self.filepath = filepath.replace("|", ".")
        self.compressed = compressed
        
        
        forward_i = filepath.rfind("/") + 1
        backwad_i = filepath.rfind("\\") + 1
        
        self.setText(0, self.filepath[max(forward_i, backwad_i):])
    
    def set_sound(self, sound_data):
        self.sound_data = sound_data
        if sound_data is not None:
            icon = QIcon("icons/sound.png")
            self.setIcon(0, icon)
        else:
            self.setIcon(0, QIcon() )
    
    def save_animation(self, other_filepath = "", compress_dis = 1, save_all = False):
        
        if save_all and not self.changed:
            print("skipping " + self.filepath + " because nothing has changed")
            return
        if other_filepath != "":
            working_filepath = other_filepath
        else:
            working_filepath = self.filepath
            
        if (working_filepath.endswith("a") and not working_filepath.endswith(".bva")  ):
            info = j3d.fix_array( self.display_info)
            self.convert_to_a(info)
        else: 
            info = j3d.fix_array( self.display_info)
            j3d.sort_filepath(working_filepath, info, self.sound_data) 
        
        compress_status = self.compressed
        if compress_dis != 0:
            compress_status = compress_dis
        print(compress_status)
        if compress_status > 1:
            out = BytesIO()
            with open(working_filepath, "rb") as f:
                if compress_status == 2:
                    out = compress_fast(f)
                elif compress_status == 3:
                    out = compress(f)
                elif compress_status == 4:
                    out = compress_slow(f)
            with open(working_filepath, "wb") as f:
                f.write(out.getbuffer())
        self.changed = False
    def convert_to_k(self):
        filepath = self.filepath[:-1] + "k"
        info = j3d.fix_array(self.display_info)  
        if self.filepath.endswith(".bca"):                     
            bck = j3d.sort_filepath(filepath, info)
        elif filepath.endswith(".bla"):             
            blk = j3d.sort_filepath(filepath, info)
        
    def convert_to_a(self, info):
    
        info = j3d.fix_array( info )
  
        if self.filepath.endswith(".bck") or self.filepath.endswith(".bca"):

         
            bca = j3d.convert_to_a(self.filepath, info) #this is a pure bck, no saving
            filepath = self.filepath[:-1] + "a"
            with open(filepath, "wb") as f:           
                bca.write_bca(f)
                f.close()
        elif self.filepath.endswith(".blk") or self.filepath.endswith(".bla"):
        
            
            bla = j3d.convert_to_a(self.filepath, info) #this is a pure bck, no saving
            filepath = self.filepath[:-1] + "a"
            with open(filepath, "wb") as f:           
                bla.write_bla(f)
                f.close()
    
    def export_anim(self):
        info = j3d.fix_array(self.display_info)  
        filepath = self.filepath[0:-4] + ".anim"
        if self.bmd_file is None:
            bmd_file, choosentype = QFileDialog.getOpenFileName( None, "Open File","" , "Model files (*.bmd *.bdl)")
            if bmd_file:
                
                bck = j3d.export_anim(filepath, info, bmd_file)
        else:
            bck = j3d.export_anim(filepath, info, self.bmd_file)
        
    def add_children(self, strings):
        self.takeChildren()
        for name in strings:
            child = QTreeWidgetItem(self)
            child.setText(0, name)
            child.setDisabled(True)
[((10, 8, 10, 51), 'PyQt5.QtWidgets.QTreeWidgetItem.__init__', 'QTreeWidgetItem.__init__', ({(10, 33, 10, 37): 'self', (10, 39, 10, 45): 'parent', (10, 46, 10, 50): '(1000)'}, {}), '(self, parent, 1000)', False, 'from PyQt5.QtWidgets import QAction, QTreeWidget, QTreeWidgetItem, QFileDialog\n'), ((73, 15, 73, 47), 'animations.general_animation.fix_array', 'j3d.fix_array', ({(73, 29, 73, 46): 'self.display_info'}, {}), '(self.display_info)', True, 'import animations.general_animation as j3d\n'), ((81, 15, 81, 36), 'animations.general_animation.fix_array', 'j3d.fix_array', ({(81, 30, 81, 34): 'info'}, {}), '(info)', True, 'import animations.general_animation as j3d\n'), ((101, 15, 101, 47), 'animations.general_animation.fix_array', 'j3d.fix_array', ({(101, 29, 101, 46): 'self.display_info'}, {}), '(self.display_info)', True, 'import animations.general_animation as j3d\n'), ((33, 19, 33, 43), 'PyQt5.QtGui.QIcon', 'QIcon', ({(33, 25, 33, 42): '"""icons/sound.png"""'}, {}), "('icons/sound.png')", False, 'from PyQt5.QtGui import QIcon\n'), ((49, 19, 49, 52), 'animations.general_animation.fix_array', 'j3d.fix_array', ({(49, 34, 49, 51): 'self.display_info'}, {}), '(self.display_info)', True, 'import animations.general_animation as j3d\n'), ((52, 19, 52, 52), 'animations.general_animation.fix_array', 'j3d.fix_array', ({(52, 34, 52, 51): 'self.display_info'}, {}), '(self.display_info)', True, 'import animations.general_animation as j3d\n'), ((53, 12, 53, 70), 'animations.general_animation.sort_filepath', 'j3d.sort_filepath', ({(53, 30, 53, 46): 'working_filepath', (53, 48, 53, 52): 'info', (53, 54, 53, 69): 'self.sound_data'}, {}), '(working_filepath, info, self.sound_data)', True, 'import animations.general_animation as j3d\n'), ((60, 18, 60, 27), 'io.BytesIO', 'BytesIO', ({}, {}), '()', False, 'from io import BytesIO\n'), ((75, 18, 75, 51), 'animations.general_animation.sort_filepath', 'j3d.sort_filepath', ({(75, 36, 75, 44): 'filepath', (75, 46, 75, 50): 'info'}, {}), '(filepath, info)', True, 'import animations.general_animation as j3d\n'), ((86, 18, 86, 55), 'animations.general_animation.convert_to_a', 'j3d.convert_to_a', ({(86, 35, 86, 48): 'self.filepath', (86, 50, 86, 54): 'info'}, {}), '(self.filepath, info)', True, 'import animations.general_animation as j3d\n'), ((104, 36, 104, 116), 'PyQt5.QtWidgets.QFileDialog.getOpenFileName', 'QFileDialog.getOpenFileName', ({(104, 65, 104, 69): 'None', (104, 71, 104, 82): '"""Open File"""', (104, 83, 104, 85): '""""""', (104, 88, 104, 115): '"""Model files (*.bmd *.bdl)"""'}, {}), "(None, 'Open File', '', 'Model files (*.bmd *.bdl)')", False, 'from PyQt5.QtWidgets import QAction, QTreeWidget, QTreeWidgetItem, QFileDialog\n'), ((109, 18, 109, 64), 'animations.general_animation.export_anim', 'j3d.export_anim', ({(109, 34, 109, 42): 'filepath', (109, 44, 109, 48): 'info', (109, 50, 109, 63): 'self.bmd_file'}, {}), '(filepath, info, self.bmd_file)', True, 'import animations.general_animation as j3d\n'), ((114, 20, 114, 41), 'PyQt5.QtWidgets.QTreeWidgetItem', 'QTreeWidgetItem', ({(114, 36, 114, 40): 'self'}, {}), '(self)', False, 'from PyQt5.QtWidgets import QAction, QTreeWidget, QTreeWidgetItem, QFileDialog\n'), ((36, 28, 36, 35), 'PyQt5.QtGui.QIcon', 'QIcon', ({}, {}), '()', False, 'from PyQt5.QtGui import QIcon\n'), ((77, 18, 77, 51), 'animations.general_animation.sort_filepath', 'j3d.sort_filepath', ({(77, 36, 77, 44): 'filepath', (77, 46, 77, 50): 'info'}, {}), '(filepath, info)', True, 'import animations.general_animation as j3d\n'), ((94, 18, 94, 55), 'animations.general_animation.convert_to_a', 'j3d.convert_to_a', ({(94, 35, 94, 48): 'self.filepath', (94, 50, 94, 54): 'info'}, {}), '(self.filepath, info)', True, 'import animations.general_animation as j3d\n'), ((107, 22, 107, 63), 'animations.general_animation.export_anim', 'j3d.export_anim', ({(107, 38, 107, 46): 'filepath', (107, 48, 107, 52): 'info', (107, 54, 107, 62): 'bmd_file'}, {}), '(filepath, info, bmd_file)', True, 'import animations.general_animation as j3d\n'), ((63, 26, 63, 42), 'widgets.yaz0.compress_fast', 'compress_fast', ({(63, 40, 63, 41): 'f'}, {}), '(f)', False, 'from widgets.yaz0 import compress, compress_slow, compress_fast\n'), ((65, 26, 65, 37), 'widgets.yaz0.compress', 'compress', ({(65, 35, 65, 36): 'f'}, {}), '(f)', False, 'from widgets.yaz0 import compress, compress_slow, compress_fast\n'), ((67, 26, 67, 42), 'widgets.yaz0.compress_slow', 'compress_slow', ({(67, 40, 67, 41): 'f'}, {}), '(f)', False, 'from widgets.yaz0 import compress, compress_slow, compress_fast\n')]