importPackage(java.awt);
importPackage(java.awt.geom);

function ScrollsScreen(info) {
    this.create(info);
}

ScrollsScreen.prototype.create = function(info) {
    this.type = "ScrollsScreen";
    this.size = info.modelInfo.size;
    this.texX = info.graphicsInfo.x;
    this.texY = info.graphicsInfo.y;
    this.uvX = info.modelInfo.uv ? (info.modelInfo.uv.x ? info.modelInfo.uv.x : this.size.x * this.texY / this.size.y) : this.size.x * this.texY / this.size.y;
    this.uvY = info.modelInfo.uv ? (info.modelInfo.uv.y ? info.modelInfo.uv.y : this.texY) : this.texY;
    this.renderType = info.modelInfo.renderType ? info.modelInfo.renderType : "light";
    this.faceDouble = info.modelInfo.faceDouble ? info.modelInfo.faceDouble : false;
    
    this.graphicsTexture = new GraphicsTexture(this.texX, this.texY);
    this.dynamicModelHolder = new DynamicModelHolder();
    
    this.speed = {
        x: info.screenInfo.speed ? (info.screenInfo.speed.x ? info.screenInfo.speed.x : 0) : 0,
        y: info.screenInfo.speed ? (info.screenInfo.speed.y ? info.screenInfo.speed.y : 0) : 0
    };
    this.running = info.screenInfo.running == false ? false : true;
    this.display = info.screenInfo.display == false ? false : true;
    
    this.shiftX = 0;
    this.shiftY = 0;
    
    this.buildModel(0, 0);
}

ScrollsScreen.prototype.buildModel = function(scrollX, scrollY) {
    let stratX = scrollX / this.texX;
    let width = this.uvX / this.texX;
    let stratY = scrollY / this.texY;
    let high = this.uvY / this.texY;
    
    let rawModel = new RawModel();
    let rawModelBuilder = new RawMeshBuilder(4, this.renderType, this.graphicsTexture.identifier);
    
    rawModelBuilder.vertex(this.size.x / 2, this.size.y / 2, 0).normal(0, 0, 1).uv(stratX + width, stratY).endVertex()
        .vertex(-this.size.x / 2, this.size.y / 2, 0).normal(0, 0, 1).uv(stratX, stratY).endVertex()
        .vertex(-this.size.x / 2, -this.size.y / 2, 0).normal(0, 0, 1).uv(stratX, stratY + high).endVertex()
        .vertex(this.size.x / 2, -this.size.y / 2, 0).normal(0, 0, 1).uv(stratX + width, stratY + high).endVertex();
    
    if (this.faceDouble) {
        rawModelBuilder.vertex(this.size.x / 2, this.size.y / 2, 0).normal(0, 0, -1).uv(stratX, stratY).endVertex()
            .vertex(this.size.x / 2, -this.size.y / 2, 0).normal(0, 0, -1).uv(stratX, stratY + high).endVertex()
            .vertex(-this.size.x / 2, -this.size.y / 2, 0).normal(0, 0, -1).uv(stratX + width, stratY + high).endVertex()
            .vertex(-this.size.x / 2, this.size.y / 2, 0).normal(0, 0, -1).uv(stratX + width, stratY).endVertex();
    }
    
    rawModel.append(rawModelBuilder.getMesh());
    rawModel.generateNormals();
    
    this.dynamicModelHolder.uploadLater(rawModel);
}

ScrollsScreen.prototype.update = function() {
    if (this.running && this.display) {
        this.shiftX += this.speed.x * Timing.delta();
        this.shiftY += this.speed.y * Timing.delta();
        
        if (this.shiftX > this.texX) {
            this.shiftX = 0;
        }
        if (this.shiftY > this.texY) {
            this.shiftY = 0;
        }
        
        this.buildModel(this.shiftX, this.shiftY);
    }
}

ScrollsScreen.prototype.graphics = function() {
    return this.graphicsTexture.graphics;
}

ScrollsScreen.prototype.upload = function() {
    this.graphicsTexture.upload();
}

ScrollsScreen.prototype.close = function() {
    if (this.graphicsTexture) {
        this.graphicsTexture.close();
    }
}

ScrollsScreen.prototype.resetScroll = function() {
    this.shiftX = 0;
    this.shiftY = 0;
    this.buildModel(0, 0);
}
