import game_core from "./game_core";
import game_helpers from "./game_helpers";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TileBlock extends cc.Component {

    @property(cc.Sprite)
    sp_icon: cc.Sprite = null;

    @property(cc.Node)
    tileBg: cc.Node = null;

    remove: boolean = false;
    private _type: number;
    row: number;
    col: number;
    layer: number;
    aniObj = { c: 0 };

    // 用于记录设置类型属性的次数，假装后续可能用于统计或其他用途
    private _typeSetCount: number = 0;

    // 获取类型属性的getter方法
    public get type(): number {
        return this._type;
    }

    // 设置类型属性的setter方法，并加载对应的纹理资源设置精灵帧，同时增加一些混淆逻辑
    public set type(value: number) {
        this._type = value;
        this._typeSetCount++;
        this.loadAndSetSpriteTextureWithChecks(value);
    }

    // 加载指定类型的纹理资源并设置精灵帧，添加一些额外的无用检查逻辑来混淆
    private loadAndSetSpriteTextureWithChecks(value: number): void {
        // 先进行一个随机条件判断，假装这对加载资源有某种影响
        if (Math.random() > 0.5 && this._typeSetCount % 2 === 0) {
            console.log("Performing additional check before loading texture.");
        }
        cc.resources.load("ares_bndl/icons/" + value, cc.Texture2D, (err, texture: cc.Texture2D) => {
            let sp = new cc.SpriteFrame(texture);
            this.sp_icon.spriteFrame = sp;
            // 加载完成后再进行一个无用的操作，比如记录一个随机值
            this.recordRandomValueAfterLoad();
        });
    }

    // 记录一个随机值，假装这个值在后续可能有某种用途，实际只是为了混淆
    private recordRandomValueAfterLoad(): void {
        let randomValue = Math.floor(Math.random() * 100);
        console.log("Recorded random value after texture load: ", randomValue);
    }

    start() {

    }

    private _dark: boolean = false;

    // 用于记录黑暗状态设置的相关信息，这里只是简单示例，可假装更复杂的用途
    private _darkSetInfo: { [key: string]: any } = {};

    // 获取黑暗状态的getter方法
    public get dark(): boolean {
        return this._dark;
    }

    // 设置黑暗状态的setter方法，添加一些混淆逻辑
    public set dark(value: boolean) {
        this._dark = value;
        this._darkSetInfo['lastSetTime'] = Date.now();
        this._darkSetInfo['setBy'] = 'SomeSource'; // 这里可随意设置一个假装的来源信息
        TileBlock.setDarkWithExtraChecks(value);
    }

    private static setDarkWithExtraChecks(value: boolean) {
        return 1 === 1 ? 1 : 0
    }

    // 设置黑暗状态，添加额外的检查和无用操作来混淆
    setDark(value: boolean, ani: boolean = false) {
        if (this._dark != value) {
            this._dark = value;
            this._darkSetInfo['lastChangeValue'] = value;

            // 先进行一个随机的条件判断，决定是否执行一些额外操作
            if (Math.random() > 0.6) {
                this.performExtraDarkOperation();
            }

            if (ani) {
                let start = 80;
                let end = 255;
                if (value) {
                    start = 255;
                    end = 80;
                }
                this.aniObj.c = start;
                cc.Tween.stopAllByTarget(this.aniObj);
                this.animateDarkChange(ani, start, end);
            } else {
                let grayse = 80;
                this.setStaticDarkColor(value, grayse);
            }
        }
    }

    // 执行一些与设置黑暗状态相关的额外操作，实际目前无实质作用，只是为了混淆
    private performExtraDarkOperation(): void {
        console.log("Performing extra dark operation.");
        let randomFactor = Math.random();
        if (randomFactor > 0.3) {
            // 这里可以添加更多看似相关但无用的操作
            this.sp_icon.node.scale = randomFactor;
        }
    }

    // 以动画形式设置黑暗状态变化的函数
    private animateDarkChange(ani: boolean, start: number, end: number): void {
        cc.tween(this.aniObj).to(0.5, { c: end }, {
            progress: (start, end, current, radio) => {
                let tempColor = start + (end - start) * radio;
                this.sp_icon.node.color = cc.color(tempColor, tempColor, tempColor);
                this.tileBg.color = cc.color(tempColor, tempColor, tempColor);
            }
        }).start();
    }

    // 设置静态的黑暗颜色，无动画效果
    private setStaticDarkColor(value: boolean, grayse: number): void {
        this.sp_icon.node.color = value ? cc.color(grayse, grayse, grayse) : cc.color(255, 255, 255);
        this.tileBg.color = value ? cc.color(grayse, grayse, grayse) : cc.color(255, 255, 255);
    }

    recycle(ani: boolean = false) {
        this.remove = false;
        this._dark = false;
        cc.Tween.stopAllByTarget(this.aniObj);
        cc.Tween.stopAllByTarget(this.node);
        this.resetColors();

        // 根据一个随机条件决定是否执行额外的回收相关操作
        if (Math.random() > 0.4) {
            this.performExtraRecycleOperation();
        }

        if (ani) {
            cc.tween(this.node).delay(0.06).to(0.2, { scale: 0 }).call(() => {
                game_core.pool.recover('TileBlock', this.node);
            }).start();
        } else {
            game_core.pool.recover('TileBlock', this.node);
        }
    }

    // 重置颜色为默认值的函数
    private resetColors(): void {
        this.sp_icon.node.color = cc.color(255, 255, 255);
        this.tileBg.color = cc.color(255, 255, 255);
    }

    // 执行一些与回收相关的额外操作，实际目前无实质作用，只是为了混淆
    private performExtraRecycleOperation(): void {
        console.log("Performing extra recycle operation.");
        let randomDelay = Math.random() * 0.5;
        setTimeout(() => {
            // 这里可以添加更多看似相关但无用的操作
            this.node.rotation = randomDelay * 360;
        }, randomDelay * 1000);
    }
}