import { _decorator, Component, Node, EditBox, EventTouch } from 'cc';
import { tgxSceneUtil, tgxUIMgr } from '../../core_tgx/tgx';
import { SceneDef } from '../../scripts/SceneDef';
import { UserMgr } from './UserMgr';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Index
 * DateTime = Tue Nov 29 2022 20:05:39 GMT+0800 (China Standard Time)
 * Author = mykylin
 * FileBasename = Index.ts
 * FileBasenameNoExtension = Index
 * URL = db://assets/scripts/Index.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

const LAST_NAMES = ['勇敢的', '怂包的', '无辜的', '残暴的', '天真的', '胆小的', '幽默的', '好色的'];
const GIVEN_NAMES = ['角斗士', '仲裁者', '看守者', '黑色教长', '格里芬', '好战者', '和平使者', '征服者', '百夫长'];

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

    @property(EditBox)
    edtName: EditBox;

    @property(Node)
    selector: Node;

    @property(Node)
    icons: Node;

    private _iconIndex = 0;

    start() {
        tgxUIMgr.inst.closeAll();
        this._iconIndex = Math.floor(Math.random() * this.icons.children.length);
        let icon = this.icons.children[this._iconIndex];
        //this.selector.setPosition(icon.position);

        this.onBtnRandomName();
    }


    onBtnRandomName() {
        let lastName = LAST_NAMES[Math.floor(Math.random() * LAST_NAMES.length)];
        let givenName = GIVEN_NAMES[Math.floor(Math.random() * GIVEN_NAMES.length)];
        this.edtName.string = lastName + givenName;
    }

    onIconSelected(event: EventTouch,data : string) {
        for (let i = 0; i < this.icons.children.length; ++i) {
            if (this.icons.children[i] == event.currentTarget) {
                this._iconIndex = Number(data) || 410001;
                let icon = this.icons.children[i];
                this.selector.setPosition(icon.position);
            }
        }
    }

    async onBtnStart() {
        let visualId = this._iconIndex;
        let name = this.edtName.string;
        let ret = await UserMgr.inst.doCreateRole(name, visualId);
        if (!ret.isSucc) {
            return;
        }
        //create role successfully, enter meta world
        //角色创建成功,进入大厅
        tgxSceneUtil.loadScene(SceneDef.LOBBY);
    }
}