import { CoroutineV2 } from "../../CatanEngine/CoroutineV2/CoroutineV2";
import UIPanel from "../../Component/UIPanel/UIPanel";
import ScrollItem from "./ScrollItem";
import UISuperLayout from "./UISuperLayout";

const { ccclass, property } = cc._decorator;

/** Scroll基底 */
@ccclass
export default class ScrollBase extends UIPanel {
	//#region 外調參數

	@property({ displayName: "List", type: UISuperLayout })
	public List: UISuperLayout = null;

	//#endregion

	//#region public

	/** ListData */
	public get ListData(): any[] { throw new Error("ListData必須被override"); }

	//#endregion

	//#region protected

	/** 列表資料 */
	protected _listData: any[] = [];

	//#endregion

	//#region ScrollView Function

	/**
	 * 創建/刷新列表
	 * @param isScrollTo 0 不滾動
	 * @param isScrollTo 1 滚动到头部
	 * @param isScrollTo 2 滚动到尾部
	 * @param isScrollTo 3 自动居中到最近Item
	 */
	public *CreateList(isScrollTo: number = 0): IterableIterator<any> {
		if (!this.node.active) {
			return;
		}
		this.List?.CreateItem(this.ListData.length);
		yield CoroutineV2.WaitTime(5 / cc.game.getFrameRate());
		switch (isScrollTo) {
			case 1: {
				this.List?.scrollToHeader(0);
				break;
			}

			case 2: {
				this.List?.scrollToFooter(0);
				break;
			}

			case 3: {
				this.List?.scrollToCenter();
				break;
			}

			default:
				break;
		}
	}

	public OnRefreshEvent(node: cc.Node, index: number): void {
		let listData: any[] = this.ListData;
		let info: any = listData[index];
		node.getComponent(ScrollItem).ImplementSet(info, this, index);
	}

	//#endregion
}