import { _decorator, instantiate, Label, Node, Prefab, SpotLight, Sprite, tween, UIOpacity } from 'cc';
import { BaseView } from '../../../framework/layer/BaseView';
import { StringUtil } from '../../../framework/util/StringUtil';
import { Equip } from '../../data/EquipData';
import { EquipConf } from '../../config/EquipConf';
import { CommonItem } from '../common/CommonItem';
import { AttrAddTypeEnum, AttrConf } from '../../common/InterfaceAddEnum';
import { Framework } from '../../../framework/Framework';
import { ViewID } from '../../../framework/config/LayerConf';
import { GameEvent } from '../../data/GameEvent';
const { ccclass, property } = _decorator;

@ccclass('EquipOpreate')
export class EquipOpreate extends BaseView {
	@property({ type: Label, tooltip: "关闭提示" })
	closeTips: Label = null;

	@property({ type: Label, tooltip: "标题" })
	titieTx: Label = null;

	@property({ type: Node, tooltip: "图标节点" })
	itemNode: Node = null;

	@property({ type: Label, tooltip: "名字" })
	nameTx: Label = null;

	@property({ type: Label, tooltip: "阵营" })
	posTx: Label = null;

	@property({ type: Label, tooltip: "部位" })
	slotTx: Label = null;

	@property({ type: Label, tooltip: "描述" })
	descTx: Label = null;

	@property({ type: Label, tooltip: "属性标题" })
	attrTitieTx: Label = null;

	@property({ type: Label, tooltip: "属性" })
	attrTx: Label = null;

	@property({ type: Sprite, tooltip: "替换/卸下按钮图" })
	wearBtnSp: Sprite = null;
	@property({ type: Label, tooltip: "替换/卸下按钮文字" })
	wearBtnTx: Label = null;

	@property({ type: Label, tooltip: "升级按钮文字" })
	updateBtnTx: Label = null;

	@property({ type: Node, tooltip: "强化按钮" })
	strongBtn: Node = null;
	@property({ type: Label, tooltip: "强化按钮文字" })
	strongBtnTx: Label = null;

	private _curRace: number = 0;
	private _curSlot: number = 0;
	private _curEquip: Equip = null;

	protected onLoad() {
		super.onLoad();
		this.closeTips.string = StringUtil.getLanguageData('点击空白关闭');
		this.titieTx.string = StringUtil.getLanguageData('装备详情');
		this.attrTitieTx.string = StringUtil.getLanguageData('属性加成');
		this.wearBtnTx.string = StringUtil.getLanguageData('替换');
		this.updateBtnTx.string = StringUtil.getLanguageData('升级');
		this.strongBtnTx.string = StringUtil.getLanguageData('突破');
		this.closeTips.node.getComponent(UIOpacity).opacity = 0;

		Framework.event.addEvent(GameEvent.EquipWearChange, () => {
			Framework.layer.close(this);
		}, this);
	}

	protected onDestroy() {

	}

	//UI开打时会调用,如果有初始化代码应该放到此函数
	onOpen(data) {
		tween(this.closeTips.node.getComponent(UIOpacity))
			.to(1, { opacity: 255 })
			.to(1.2, { opacity: 10 })
			.union()
			.repeatForever()
			.start()

		this._curRace = data.race;
		this._curSlot = data.slot;
		this._curEquip = data.equip;
		if (this._curEquip.conf['Quality'] < 5) {
			this.strongBtn.active = false;
		}
		this.updateUI();
	}

	//UI关闭时会调用,该函数在onDestroy前调用
	onClose() {

	}

	//框架管理UI层级时会调用,可根据UI情况修改
	onShow() {
		super.onShow();

	}

	//框架管理UI层级时会调用,可根据UI情况修改
	onHide() {
		super.onHide();
	}

	//UI事件处理
	private onTouchButton(event: Event) {
		//Framework.audio.playEffect(AudioID.Click);
		let target: any = event.target;
		if (target.name == 'mask') {
			Framework.layer.close(this);
		} else if (target.name == 'wear_btn') {
			let args = { race: this._curRace, slot: this._curSlot, equip: this._curEquip }
			Framework.layer.open(ViewID.EquipChoose, null, args);
		} else if (target.name == 'update_btn') {
			let args = { race: this._curRace, slot: this._curSlot, equip: this._curEquip }
			Framework.layer.open(ViewID.EquipUpdate, null, args);
		} else if (target.name == 'strong_btn') {
			// let args = { race: this._curRace, slot: this._curSlot, equip: this._curEquip }
			// Framework.layer.open(ViewID.EquipStrong, null, args);
		}
	}

	updateUI() {

		this.load('common', `prefab/CommonItem`, Prefab, (pre: Prefab) => {
			let item = instantiate(pre);
			this.itemNode.addChild(item);
			item.getComponent(CommonItem).setClickEnable(false);
			item.getComponent(CommonItem).setNumShow(false);
			item.getComponent(CommonItem).refreshItem(this._curEquip);
		})

		this.nameTx.string = `${StringUtil.getLanguageData('装备名称')}: ${StringUtil.getLanguageData(this._curEquip.conf['Name'])}`;
		this.posTx.string = `${StringUtil.getLanguageData('阵营类型')}: ${StringUtil.getLanguageData('阵营' + this._curEquip.conf['Race'])}`;
		this.slotTx.string = `${StringUtil.getLanguageData('装备位置')}: ${StringUtil.getLanguageData('部位' + this._curEquip.conf['Slot'])}`;
		this.descTx.string = StringUtil.getLanguageData(this._curEquip.conf['Desc']);

		let attrStr = "";
		for (let index = 1; index <= 2; index++) {
			let Stat = this._curEquip.conf['Stat' + index];
			if (Stat != 0) {
				let attr = Stat.split(':');
				let attrConf = AttrConf[attr[0]]
				let value = attrConf.type == AttrAddTypeEnum.reality ? ("+" + attr[1]) :
					("+" + (100 * Number(attr[1])) + "%")
				attrStr = attrStr + StringUtil.getLanguageData(attrConf.name) + ' ' + value + "\n";
			}
		}
		this.attrTx.string = attrStr;
	}

}