package net.northking.cctp.upperComputer.driver.harmony.ui;


import net.northking.cctp.upperComputer.driver.protocol.packet.CodecUtils;
import net.northking.cctp.upperComputer.driver.protocol.packet.ICommandData;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * UI元素属性
 */
public class UiAttribute implements ICommandData {
    private String abilityName;
    private long accessibilityId;
    private String backgroundColor;
    private String backgroundImage;
    private String blur;
    /**
     * 大小
     * 非空
     */
    private String bounds;
    private UiRect boundRect = null;
    private String bundleName;
    private boolean checkable = false;
    private String description;
    private boolean enabled = false;
    private boolean focused = false;
    private String hashcode;
    private String hierarchy;
    private String hint;
    private String hitTestBehavior;
    private int hostWindowId;
    private String id;
    private String key;
    private boolean longClickable = false;
    private double opacity;
    private String origBounds;
    private UiRect originalBoundRect = null;
    private String pagePath;
    private boolean scrollable = false;
    private boolean selected = false;
    private String text;
    private String type;
    private boolean visible = false;
    private int zIndex;

    public UiAttribute() {

    }

    public UiAttribute(DataInput dataInput) throws IOException {
        buildFromInput(dataInput);
    }

    private boolean parseBooleanValue(String value) {
        return "true".equals(value);
    }

    public String getAbilityName() {
        return abilityName;
    }

    public long getAccessibilityId() {
        return accessibilityId;
    }

    public String getBackgroundColor() {
        return backgroundColor;
    }

    public String getBackgroundImage() {
        return backgroundImage;
    }

    /**
     * 模糊像素
     * <br>
     * 例如0.00px
     *
     * @return
     */
    public String getBlur() {
        return blur;
    }

    public UiRect getBoundRect() {
        if (boundRect == null) {
            boundRect = new UiRect(bounds);
        }
        return boundRect;
    }

    public String getBundleName() {
        return bundleName;
    }

    public boolean isCheckable() {
        return checkable;
    }

    public String getDescription() {
        return description;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public boolean isFocused() {
        return focused;
    }

    public String getHash() {
        return hashcode;
    }

    /**
     * 层级信息
     * <br>
     * 例如：ROOT34,0,2,0,0,0,0,0,0,0,0
     *
     * @return
     */
    public String getHierarchy() {
        return hierarchy;
    }

    public String getHint() {
        return hint;
    }

    public String getHitTestBehavior() {
        return hitTestBehavior;
    }

    public int getHostWindowId() {
        return hostWindowId;
    }

    public String getId() {
        return id;
    }

    public String getKey() {
        return key;
    }

    public boolean isLongClickable() {
        return longClickable;
    }

    public double getOpacity() {
        return opacity;
    }

    public UiRect getOriginalBoundRect() {
        if (originalBoundRect == null) {
            originalBoundRect = new UiRect(origBounds);
        }
        return originalBoundRect;
    }

    public String getPagePath() {
        return pagePath;
    }

    public boolean isScrollable() {
        return scrollable;
    }

    public boolean isSelected() {
        return selected;
    }

    public String getText() {
        return text;
    }

    public String getType() {
        return type;
    }

    public boolean isVisible() {
        return visible;
    }

    public int getZIndex() {
        return zIndex;
    }

    @Override
    public String toString() {
        return "UiAttribute{" +
                "abilityName='" + abilityName + '\'' +
                ", accessibilityId='" + accessibilityId + '\'' +
                ", backgroundColor='" + backgroundColor + '\'' +
                ", backgroundImage='" + backgroundImage + '\'' +
                ", blur='" + blur + '\'' +
                ", bounds='" + bounds + '\'' +
                ", bundleName='" + bundleName + '\'' +
                ", checkable='" + checkable + '\'' +
                ", description='" + description + '\'' +
                ", enabled='" + enabled + '\'' +
                ", focused='" + focused + '\'' +
                ", hashcode='" + hashcode + '\'' +
                ", hierarchy='" + hierarchy + '\'' +
                ", hint='" + hint + '\'' +
                ", hitTestBehavior='" + hitTestBehavior + '\'' +
                ", hostWindowId='" + hostWindowId + '\'' +
                ", id='" + id + '\'' +
                ", key='" + key + '\'' +
                ", longClickable='" + longClickable + '\'' +
                ", opacity='" + opacity + '\'' +
                ", origBounds='" + origBounds + '\'' +
                ", pagePath='" + pagePath + '\'' +
                ", scrollable='" + scrollable + '\'' +
                ", selected='" + selected + '\'' +
                ", text='" + text + '\'' +
                ", type='" + type + '\'' +
                ", visible='" + visible + '\'' +
                ", zIndex='" + zIndex + '\'' +
                '}';
    }

    @Override
    public void buildFromInput(DataInput dataInput) throws IOException {
        abilityName = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        accessibilityId = dataInput.readLong();
        backgroundColor = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        backgroundImage = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        blur = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        boundRect = new UiRect(dataInput);
        bounds = boundRect.toString();
        bundleName = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        checkable = dataInput.readBoolean();
        description = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        enabled = dataInput.readBoolean();
        focused = dataInput.readBoolean();
        hashcode = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        hierarchy = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        hint = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        hitTestBehavior = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        hostWindowId = dataInput.readInt();
        id = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        key = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        longClickable = dataInput.readBoolean();
        opacity = dataInput.readDouble();
        originalBoundRect = new UiRect(dataInput);
        origBounds = originalBoundRect.toString();
        pagePath = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        scrollable = dataInput.readBoolean();
        selected = dataInput.readBoolean();
        text = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        type = CodecUtils.readNullable(dataInput, CodecUtils::readText);
        visible = dataInput.readBoolean();
        zIndex = dataInput.readInt();
    }

    @Override
    public void encodeToOutput(DataOutput dataOutput) throws IOException {
        CodecUtils.writeNullable(dataOutput, abilityName, CodecUtils::writeText);
        dataOutput.writeLong(accessibilityId);
        CodecUtils.writeNullable(dataOutput, backgroundColor, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, backgroundImage, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, blur, CodecUtils::writeText);
        getBoundRect().encodeToOutput(dataOutput);
        CodecUtils.writeNullable(dataOutput, bundleName, CodecUtils::writeText);
        dataOutput.writeBoolean(checkable);
        CodecUtils.writeNullable(dataOutput, description, CodecUtils::writeText);
        dataOutput.writeBoolean(enabled);
        dataOutput.writeBoolean(focused);
        CodecUtils.writeNullable(dataOutput, hashcode, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, hierarchy, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, hint, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, hitTestBehavior, CodecUtils::writeText);
        dataOutput.writeInt(hostWindowId);
        CodecUtils.writeNullable(dataOutput, id, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, key, CodecUtils::writeText);
        dataOutput.writeBoolean(longClickable);
        dataOutput.writeDouble(opacity);
        getOriginalBoundRect().encodeToOutput(dataOutput);
        CodecUtils.writeNullable(dataOutput, pagePath, CodecUtils::writeText);
        dataOutput.writeBoolean(scrollable);
        dataOutput.writeBoolean(selected);
        CodecUtils.writeNullable(dataOutput, text, CodecUtils::writeText);
        CodecUtils.writeNullable(dataOutput, type, CodecUtils::writeText);
        dataOutput.writeBoolean(visible);
        dataOutput.writeInt(zIndex);
    }
}
