"""Tests for editing functionality."""

import os
import tempfile

import pytest

from py_xmind16.workbook import Workbook
from py_xmind16.sheet import Sheet
from py_xmind16.topic import Topic


class TestEditTopic:
    """Tests for editing topics."""
    
    def test_modify_topic_title(self):
        """Test modifying a topic's title."""
        topic = Topic(title="Original")
        topic.title = "Modified"
        
        assert topic.title == "Modified"
    
    def test_modify_topic_notes(self):
        """Test modifying a topic's notes."""
        topic = Topic(title="Test")
        topic.notes = "Initial notes"
        topic.notes = "Updated notes"
        
        assert topic.notes == "Updated notes"
    
    def test_add_and_remove_labels(self):
        """Test adding and removing labels."""
        topic = Topic(title="Test")
        
        topic.add_label("label1")
        topic.add_label("label2")
        assert len(topic.labels) == 2
        
        topic.remove_label("label1")
        assert len(topic.labels) == 1
        assert "label2" in topic.labels
    
    def test_add_and_remove_markers(self):
        """Test adding and removing markers."""
        topic = Topic(title="Test")
        
        topic.add_marker("marker1")
        topic.add_marker("marker2")
        assert len(topic.markers) == 2
        
        topic.remove_marker("marker1")
        assert len(topic.markers) == 1
        assert "marker2" in topic.markers


class TestEditHierarchy:
    """Tests for editing topic hierarchy."""
    
    def test_reparent_subtopic(self):
        """Test moving a subtopic to a different parent."""
        root = Topic(title="Root")
        parent1 = root.add_subtopic("Parent 1")
        parent2 = root.add_subtopic("Parent 2")
        child = parent1.add_subtopic("Child")
        
        # Move child from parent1 to parent2
        parent1.remove_subtopic(child)
        parent2_child = parent2.add_subtopic("Child")
        
        assert len(parent1.subtopics) == 0
        assert len(parent2.subtopics) == 1
        assert parent2_child.title == "Child"
    
    def test_deep_hierarchy_modification(self):
        """Test modifying a deep hierarchy."""
        root = Topic(title="Root")
        
        # Build deep hierarchy
        current = root
        for i in range(5):
            current = current.add_subtopic(f"Level {i}")
        
        # Modify at different levels
        level2 = root.find_subtopic_recursive("Level 2")
        level2.title = "Modified Level 2"
        level2.notes = "Added notes"
        
        assert level2.title == "Modified Level 2"
        assert level2.notes == "Added notes"


class TestEditSheet:
    """Tests for editing sheets."""
    
    def test_modify_sheet_title(self):
        """Test modifying a sheet's title."""
        sheet = Sheet(title="Original")
        sheet.title = "Modified"
        
        assert sheet.title == "Modified"
    
    def test_replace_root_topic(self):
        """Test replacing the root topic."""
        sheet = Sheet()
        old_root = sheet.get_root_topic()
        old_root.title = "Old Root"
        
        new_root = Topic(title="New Root")
        sheet.set_root_topic(new_root)
        
        assert sheet.root_topic == new_root
        assert sheet.root_topic.title == "New Root"


class TestEditWorkbook:
    """Tests for editing workbooks."""
    
    def test_modify_and_save(self):
        """Test modifying and saving a workbook."""
        workbook = Workbook()
        sheet = workbook.create_sheet("Original")
        root = sheet.get_root_topic()
        root.title = "Original Root"
        
        with tempfile.NamedTemporaryFile(suffix='.xmind', delete=False) as f:
            temp_path = f.name
        
        try:
            workbook.save(temp_path)
            
            # Load and modify
            loaded = Workbook.load(temp_path)
            loaded.get_sheet(0).title = "Modified"
            loaded.get_sheet(0).root_topic.title = "Modified Root"
            loaded.get_sheet(0).root_topic.add_subtopic("New Child")
            
            loaded.save(temp_path)
            
            # Verify modifications
            reloaded = Workbook.load(temp_path)
            assert reloaded.get_sheet(0).title == "Modified"
            assert reloaded.get_sheet(0).root_topic.title == "Modified Root"
            assert len(reloaded.get_sheet(0).root_topic.subtopics) == 1
        finally:
            if os.path.exists(temp_path):
                os.remove(temp_path)
    
    def test_add_remove_sheets(self):
        """Test adding and removing sheets."""
        workbook = Workbook()
        
        sheet1 = workbook.create_sheet("Sheet 1")
        sheet2 = workbook.create_sheet("Sheet 2")
        sheet3 = workbook.create_sheet("Sheet 3")
        
        assert workbook.sheet_count == 3
        
        workbook.remove_sheet(sheet2)
        
        assert workbook.sheet_count == 2
        assert sheet1 in workbook.sheets
        assert sheet2 not in workbook.sheets
        assert sheet3 in workbook.sheets


class TestEditPersistence:
    """Tests for edit persistence through save/load cycles."""
    
    def test_title_edit_persistence(self):
        """Test that title edits persist."""
        workbook = Workbook()
        sheet = workbook.create_sheet()
        root = sheet.get_root_topic()
        root.title = "Initial"
        
        with tempfile.NamedTemporaryFile(suffix='.xmind', delete=False) as f:
            temp_path = f.name
        
        try:
            workbook.save(temp_path)
            
            loaded = Workbook.load(temp_path)
            loaded.get_sheet(0).root_topic.title = "Edited"
            loaded.save(temp_path)
            
            reloaded = Workbook.load(temp_path)
            assert reloaded.get_sheet(0).root_topic.title == "Edited"
        finally:
            if os.path.exists(temp_path):
                os.remove(temp_path)
    
    def test_hierarchy_edit_persistence(self):
        """Test that hierarchy edits persist."""
        workbook = Workbook()
        sheet = workbook.create_sheet()
        root = sheet.get_root_topic()
        root.title = "Root"
        
        with tempfile.NamedTemporaryFile(suffix='.xmind', delete=False) as f:
            temp_path = f.name
        
        try:
            workbook.save(temp_path)
            
            loaded = Workbook.load(temp_path)
            loaded_root = loaded.get_sheet(0).root_topic
            
            # Add children to loaded workbook
            loaded_root.add_subtopic("Child 1")
            loaded_root.add_subtopic("Child 2")
            child3 = loaded_root.add_subtopic("Child 3")
            child3.add_subtopic("Grandchild")
            
            loaded.save(temp_path)
            
            reloaded = Workbook.load(temp_path)
            reloaded_root = reloaded.get_sheet(0).root_topic
            
            assert len(reloaded_root.subtopics) == 3
            child3_reloaded = reloaded_root.find_subtopic("Child 3")
            assert child3_reloaded is not None
            assert len(child3_reloaded.subtopics) == 1
        finally:
            if os.path.exists(temp_path):
                os.remove(temp_path)
    
    def test_attributes_edit_persistence(self):
        """Test that attribute edits persist."""
        workbook = Workbook()
        sheet = workbook.create_sheet()
        root = sheet.get_root_topic()
        root.title = "Root"
        
        with tempfile.NamedTemporaryFile(suffix='.xmind', delete=False) as f:
            temp_path = f.name
        
        try:
            workbook.save(temp_path)
            
            loaded = Workbook.load(temp_path)
            loaded_root = loaded.get_sheet(0).root_topic
            
            loaded_root.notes = "Added notes"
            loaded_root.add_label("label1")
            loaded_root.add_marker("marker1")
            
            loaded.save(temp_path)
            
            reloaded = Workbook.load(temp_path)
            reloaded_root = reloaded.get_sheet(0).root_topic
            
            assert reloaded_root.notes == "Added notes"
            assert "label1" in reloaded_root.labels
            assert "marker1" in reloaded_root.markers
        finally:
            if os.path.exists(temp_path):
                os.remove(temp_path)
