package dev.bytevibe.hyperpoint;

/**
 * 修改对象属性命令（除位置和尺寸外的其他属性）
 */
public class ModifyObjectPropertyCommand implements Command {
    private DrawableObject object;
    private String oldText;
    private String newText;
    private String oldFontFamily;
    private String newFontFamily;
    private double oldFontSize;
    private double newFontSize;
    private String oldFontStyle;
    private String newFontStyle;
    private String oldTextColor;
    private String newTextColor;
    private String oldFillColor;
    private String newFillColor;
    private String oldStrokeColor;
    private String newStrokeColor;
    private double oldStrokeWidth;
    private double newStrokeWidth;
    private double oldRotation;
    private double newRotation;
    private PropertyType propertyType;

    // 定义属性类型枚举
    public enum PropertyType {
        TEXT, FONT, COLOR, ROTATION, SHAPE_STYLE
    }

    // 文本内容修改构造函数
    public ModifyObjectPropertyCommand(TextObject object, String oldText, String newText, int nothing) {
        this.object = object;
        this.oldText = oldText;
        this.newText = newText;
        this.propertyType = PropertyType.TEXT;
    }

    // 字体样式修改构造函数
    public ModifyObjectPropertyCommand(TextObject object, 
                                      String oldFontFamily, String newFontFamily,
                                      double oldFontSize, double newFontSize,
                                      String oldFontStyle, String newFontStyle) {
        this.object = object;
        this.oldFontFamily = oldFontFamily;
        this.newFontFamily = newFontFamily;
        this.oldFontSize = oldFontSize;
        this.newFontSize = newFontSize;
        this.oldFontStyle = oldFontStyle;
        this.newFontStyle = newFontStyle;
        this.propertyType = PropertyType.FONT;
    }

    // 文本颜色修改构造函数
    public ModifyObjectPropertyCommand(TextObject object, String oldTextColor, String newTextColor) {
        this.object = object;
        this.oldTextColor = oldTextColor;
        this.newTextColor = newTextColor;
        this.propertyType = PropertyType.COLOR;
    }

    // 形状样式修改构造函数
    public ModifyObjectPropertyCommand(ShapeObject object, 
                                      String oldFillColor, String newFillColor,
                                      String oldStrokeColor, String newStrokeColor,
                                      double oldStrokeWidth, double newStrokeWidth) {
        this.object = object;
        this.oldFillColor = oldFillColor;
        this.newFillColor = newFillColor;
        this.oldStrokeColor = oldStrokeColor;
        this.newStrokeColor = newStrokeColor;
        this.oldStrokeWidth = oldStrokeWidth;
        this.newStrokeWidth = newStrokeWidth;
        this.propertyType = PropertyType.SHAPE_STYLE;
    }

    // 旋转属性修改构造函数
    public ModifyObjectPropertyCommand(DrawableObject object, double oldRotation, double newRotation) {
        this.object = object;
        this.oldRotation = oldRotation;
        this.newRotation = newRotation;
        this.propertyType = PropertyType.ROTATION;
    }

    @Override
    public void execute() {
        applyChanges(newText, newFontFamily, newFontSize, newFontStyle, 
                    newTextColor, newFillColor, newStrokeColor, newStrokeWidth, newRotation);
    }

    @Override
    public void undo() {
        applyChanges(oldText, oldFontFamily, oldFontSize, oldFontStyle, 
                    oldTextColor, oldFillColor, oldStrokeColor, oldStrokeWidth, oldRotation);
    }

    private void applyChanges(String text, String fontFamily, double fontSize, String fontStyle,
                             String textColor, String fillColor, String strokeColor, 
                             double strokeWidth, double rotation) {
        // 应用旋转（所有对象都有旋转属性）
        object.setRotation(rotation);
        
        // 根据对象类型应用其他属性
        if (object instanceof TextObject) {
            TextObject textObj = (TextObject) object;
            if (propertyType == PropertyType.TEXT) {
                textObj.setText(text);
            } else if (propertyType == PropertyType.FONT) {
                textObj.setFontFamily(fontFamily);
                textObj.setFontSize(fontSize);
                textObj.setFontStyle(fontStyle);
            } else if (propertyType == PropertyType.COLOR) {
                textObj.setTextColor(textColor);
            }
        } else if (object instanceof ShapeObject && propertyType == PropertyType.SHAPE_STYLE) {
            ShapeObject shapeObj = (ShapeObject) object;
            shapeObj.setFillColor(fillColor);
            shapeObj.setStrokeColor(strokeColor);
            shapeObj.setStrokeWidth(strokeWidth);
        }
    }
}