import { CCBoolean, CCInteger, CCObject, Component, Enum, find, instantiate, Node, Prefab, ScrollViewComponent, _decorator } from 'cc';
import { LiangLiangSDK } from '../../../libs/liangliangSDK';
import { ExportItem } from './ExportItem';
const { ccclass, property } = _decorator;

export enum ScrollType {
    Horizontal = 1,
    Vertical = 2
}

@ccclass('ExportList')
export class ExportList extends Component {

    @property({
        type: ScrollViewComponent,
        tooltip: "项预制体"
    })
    scrollView: ScrollViewComponent = null;

    @property({
        type: Node,
        tooltip: "项预制体"
    })
    content: Node = null;

    @property({
        type: Prefab,
        tooltip: "项预制体"
    })
    itemPrefab: Prefab = null;

    @property({
        type: Enum(ScrollType),
        tooltip: "滚动方式"
    })
    scrollType: ScrollType = ScrollType.Horizontal;

    @property({
        type: CCBoolean,
        tooltip: "是否开启自动滚动"
    })
    autoScroll:boolean = true;
    
    @property({
        type: CCInteger,
        tooltip: "滚动速度"
    })
    scrollSpeed: number = 50;

    @property({
        type: CCInteger,
        tooltip: "列表数据数量"
    })
    dataCount: number = 9;

    //前进还是后退
    isAdvance: boolean = true;

    /**
     * 是否在手动拖拽中
     */
    isHandScroll: boolean = false;

    private __items: ExportItem[];
    start() {
        this.scrollView.node.on('scroll-began', this.onScrollbegin, this);
        this.scrollView.node.on('scroll-ended', this.onScrollend, this);
        this.scrollView.node.on('scrolling', this.onScrolling, this);

        this.updataList();
    }
    
    updataList(): void {
        //删除老的内容
        this.content.removeAllChildren();
        this.__items = [];
        let adList: any[] = LiangLiangSDK.GetList(this.dataCount);
        console.log("更新导出列表", adList);
        let itemNode: Node;
        let item: ExportItem;
        let data: any;
        for (let index = 0; index < adList.length; index++) {
            data = adList[index];
            itemNode = instantiate(this.itemPrefab);
            item = itemNode.getComponent(ExportItem);
            this.__items.push(item);
            this.content.addChild(itemNode);
            item.updateItem(data);
        }
        this.scrollView.scheduleOnce(this.__scroll.bind(this), 0.5);
    }

    /**
     * 滚动开始
     * @param scrollView 
     */
    private onScrollbegin(scrollView): void {
        this.scrollView.stopAutoScroll();
    }

    /**
     * 滚动结束
     * @param scrollView 
     */
    private onScrollend(scrollView): void {
        //手动控制
        if (this.isHandScroll) {
            this.scheduleOnce(this.__scroll.bind(this), 1);
        } else {
            this.isAdvance = !this.isAdvance;
            this.scheduleOnce(this.__scroll.bind(this), 3.0);
        }
        this.isHandScroll = false;
    }

    private __scroll(): void {
        let second: number;
        if (this.isAdvance) {
            if (this.scrollType == ScrollType.Horizontal) {
                second = (this.scrollView.getMaxScrollOffset().x - Math.abs(this.scrollView.getScrollOffset().x)) / this.scrollSpeed;
            } else {
                second = (this.scrollView.getMaxScrollOffset().y - Math.abs(this.scrollView.getScrollOffset().y)) / this.scrollSpeed;
            }
            if (second > 0) {
                if (this.scrollType == ScrollType.Horizontal) {
                    this.scrollView.scrollToRight(second, false);
                } else {
                    this.scrollView.scrollToBottom(second, false);
                }
            }
            else {
                this.onScrollend(null);
            }
        }
        else {
            if (this.scrollType == ScrollType.Horizontal) {
                second = Math.abs(this.scrollView.getScrollOffset().x) / this.scrollSpeed;
            } else {
                second = Math.abs(this.scrollView.getScrollOffset().y) / this.scrollSpeed;
            }
            if (second > 0) {
                if (this.scrollType == ScrollType.Horizontal) {
                    this.scrollView.scrollToLeft(second, false);
                } else {
                    this.scrollView.scrollToTop(second, false);
                }
            }
            else {
                this.onScrollend(null);
            }
        }
    }
    /**
     * 滚动中
     * @param scrollView 
     */
    private onScrolling(scrollView): void {
        if (this.scrollView.isScrolling()) {
            this.isHandScroll = true;
        }
    }

    update(deltaTime: number) {

    }
}