import { constants } from "node:buffer";
import { ShapeRect, ShapeRectNew } from "./Unit";

export class Scrollbar {

    scaleX = 1;

    scaleY = 1;

    /**视窗高度 */
    viewHight = 0;

    /**总高度 */
    hight = 0;

    bg: ShapeRect;

    shown = true;

    scrollbar: ShapeRectNew;

    scrollbarBg: ShapeRectNew;

    scrollbarInitY = 0;

    width = 0;

    canMoveDis = 0;

    perRowDis = 0;

    /**触摸y坐标 */
    touchY = 0;

    touched = false;

    curY = 0;

    /**滚动比例 */
    scale = 1;

    init(scaleX = 1, scaleY = 1, viewHight = 0, hight = 0, width = 0) {
        this.scaleX = scaleX;
        this.scaleY = scaleY;

        this.viewHight = viewHight;
 
        this.hight = hight;

        this.width = width;

        this.initBg();

        this.initView();
    }

    private initBg() {

        const bg = new ShapeRect();
        bg.setSize(this.width * this.scaleX, this.viewHight);
        bg.alpha = 0;
        this.bg = bg;
    }

    private initView() {
        const scrollbarBg = new ShapeRectNew();
        scrollbarBg.setSize(this.width * this.scaleX, this.viewHight, 7.5 * this.scaleX);
        scrollbarBg.fillColor = "#000000";
        scrollbarBg.x = 0;
        scrollbarBg.y = 0;
        scrollbarBg.alpha = 0;
        this.scrollbarBg = scrollbarBg;
        this.bg.addChild(scrollbarBg);

        const scrollbar = new ShapeRectNew();
        scrollbar.setSize(8 * this.scaleX, 0 * this.scaleY, 4 * this.scaleX);
        scrollbar.fillColor = "#fef89a";
        scrollbar.x = (this.bg.width - scrollbar.width) / 2;
        scrollbar.y = 3 * this.scaleY;
        scrollbar.visible = false;
        this.bg.addChild(scrollbar);
        this.scrollbar = scrollbar;
        this.scrollbarInitY = scrollbar.y;

    }

    hide(){
        this.bg.visible = false;
        this.shown = false;
    }

    show(){
        this.bg.visible = true;
        this.shown = true;
    }

    showScrollbar() {

        if (this.hight <= this.viewHight) {
            this.scrollbarBg.alpha = 0;
            this.scrollbar.visible = false;
        }
        else {
            this.scrollbarBg.alpha = 1;
            this.scrollbar.visible = true;

            let draghight = this.viewHight - 6 * this.scaleY;

            let height = draghight / (this.hight / draghight);

            this.scrollbar.height = height;

            this.canMoveDis = draghight - height;

            if(this.canMoveDis != 0){
                this.scale = (this.hight - draghight) / this.canMoveDis
            } 
        }
    }

    touchScrollbar(my: number) {
        this.touchY = my;
        this.curY = this.scrollbar.y;
        this.touched = true;
    }

    drag(dis: number, callback) {

        if (!this.touched) {
            return;
        }
        let newY = this.curY + dis;

        if (newY < this.scrollbarInitY) {
            newY = this.scrollbarInitY;
        }
        else if (newY > this.scrollbarInitY + this.canMoveDis) {
            newY = this.scrollbarInitY + this.canMoveDis;
        }

        this.scrollbar.y = newY;

        let tDis = newY - this.curY;

        if(callback){
            let dis = tDis * this.scale;
            callback(dis);
        }
    }

    setOffset(dis){

        let newY = this.scrollbar.y + dis / this.scale;

        if (newY < this.scrollbarInitY) {
            newY = this.scrollbarInitY;
        }
        else if (newY > this.scrollbarInitY + this.canMoveDis) {
            newY = this.scrollbarInitY + this.canMoveDis;
        }

        this.scrollbar.y = newY;
        
    }
}