import { _decorator, instantiate, Label, Node, Prefab, ProgressBar, tween, UIOpacity } from 'cc';
import { BaseView } from '../../../framework/layer/BaseView';
import { StringUtil } from '../../../framework/util/StringUtil';
import { CommonItem } from '../common/CommonItem';
import List from '../../../framework/list/List';
import { Equip } from '../../data/EquipData';
import { EquipManager } from '../../manager/EquipManager';
import { AttrAddTypeEnum, AttrConf } from '../../common/InterfaceAddEnum';
import { Framework } from '../../../framework/Framework';
const { ccclass, property } = _decorator;

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

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

	@property({ type: Label, tooltip: "装备名" })
	equipNameTx: Label = null;

	@property({ type: Node, tooltip: "装备节点" })
	equipNode: Node = null;

	@property({ type: Label, tooltip: "当前等级" })
	equipNowLevel: Label = null;

	@property({ type: Label, tooltip: "目标等级" })
	equipNextLevel: Label = null;

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

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

	@property({ type: Label, tooltip: "目标属性加成" })
	equipNextAttrAdd: Label = null;

	@property({ type: Node, tooltip: "等级箭头" })
	levelArrow: Label = null;

	@property({ type: Node, tooltip: "属性箭头" })
	attrArrow: Node = null;

	@property({ type: Label, tooltip: "经验值标题" })
	expTitle: Label = null;

	@property({ type: ProgressBar, tooltip: "经验值进度" })
	expBar: ProgressBar = null;

	@property({ type: Label, tooltip: "经验值文字" })
	expBarTx: Label = null;

	@property({ type: List, tooltip: "材料列表" })
	itemSv: List = null;

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

	@property({ type: Label, tooltip: "一键按钮文字" })
	autoBtnTx: Label = null;

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

	private _curExp: number = 0;
	private _otherExp: number = 0;

	private _equipsData = [];

	private _selectEquip = {};

	protected onLoad() {
		super.onLoad();
		this.closeTips.string = StringUtil.getLanguageData('点击空白关闭');
		this.titieTx.string = StringUtil.getLanguageData('装备升级');
		this.updateBtnTx.string = StringUtil.getLanguageData('升级');
		this.autoBtnTx.string = StringUtil.getLanguageData('一键选择');
		this.closeTips.node.getComponent(UIOpacity).opacity = 0;
	}

	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;

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

		this.equipNameTx.string = StringUtil.getLanguageData(this._curEquip.conf['Name']);
		this._curExp = this._curEquip.exp;

		this._equipsData = EquipManager.getEquipRaceSlotAllGroup(data.race);

		this.itemSv.numItems = this._equipsData.length;

		this.initUI();
	}

	//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 == 'update_btn') {
			if (Object.keys(this._selectEquip).length <= 0) {
				Framework.tips.setTips(StringUtil.getLanguageData('请选择吞噬的装备'));
				return;
			}
			let args = {
				race: this._curRace,
				slot: this._curSlot,
				eat_eids: {}
			}
			for (const key in this._selectEquip) {
				if (Object.prototype.hasOwnProperty.call(this._selectEquip, key)) {
					const element = this._selectEquip[key];
					args.eat_eids[key] = element.count;
				}
			}
			EquipManager.sendUpdateMsg(args, () => {
				Framework.tips.setTips(StringUtil.getLanguageData('升级成功!'));
				Framework.layer.close(this);
			})
		} else if (target.name == 'auto_btn') {

		}
	}

	onEventList(item, idx) {
		item.getComponent(CommonItem).refreshItem(this._equipsData[idx]);
		item.getComponent(CommonItem).setClick((data, isSelect) => {
			item.getComponent(CommonItem).setSelectEnable(isSelect);
			if (isSelect) {
				this._selectEquip[data.id] = data;
				this._otherExp = this._otherExp + ((data.conf.EatExp + data.exp) * data.count);
			} else {
				if (this._selectEquip.hasOwnProperty(data.id)) {
					delete this._selectEquip[data.id];
					this._otherExp = this._otherExp - ((data.conf.EatExp + data.exp) * data.count);
				}
			}
			this.updateByExp();
		});
	}

	initUI() {
		let curLvConf = EquipManager.getEquipLevelByExp(this._curExp);
		this.equipNowLevel.string = 'Lv.' + curLvConf.Id;
		let attr = this._curEquip.conf['Stat1'].split(':');
		let attrConf = AttrConf[attr[0]]
		let attrStr = StringUtil.getLanguageData(attrConf.name);
		let nowAdd = null;
		let value = null;
		if (attrConf.type == AttrAddTypeEnum.reality) {
			nowAdd = attr[1] * (1 + curLvConf.AttackMod);
			value = " +" + parseFloat((attr[1] * (1 + curLvConf.AttackMod)).toFixed(2));
		} else {
			nowAdd = 100 * Number(attr[1]) * (1 + curLvConf.AttackMod);
			value = " +" + parseFloat((100 * Number(attr[1]) * (1 + curLvConf.AttackMod)).toFixed(2)) + "%";
		}
		attrStr = attrStr + value;
		this.equipNowAttr.string = attrStr;

		let nextLvConf = EquipManager.getEquipNextLevel(this._curExp);
		this.equipNextLevel.string = 'Lv.' + nextLvConf.Id;
		let attrNextConf = AttrConf[attr[0]]
		let attrNextStr = StringUtil.getLanguageData(attrNextConf.name);
		let nextAdd = null;
		let valueNext = null;
		if (attrConf.type == AttrAddTypeEnum.reality) {
			nextAdd = attr[1] * (1 + nextLvConf.AttackMod);
			valueNext = " +" + parseFloat((attr[1] * (1 + nextLvConf.AttackMod)).toFixed(2));
		} else {
			nextAdd = 100 * Number(attr[1]) * (1 + nextLvConf.AttackMod);
			valueNext = " +" + parseFloat((100 * Number(attr[1]) * (1 + nextLvConf.AttackMod)).toFixed(2)) + "%";
		}
		attrNextStr = attrNextStr + valueNext;
		this.equipNextAttr.string = attrNextStr;

		this.equipNextAttrAdd.string = attrConf.type == AttrAddTypeEnum.reality ? (" (+" + parseFloat((nextAdd - nowAdd).toFixed(2)) + ")") :
			(" (+" + parseFloat((nextAdd - nowAdd).toFixed(2)) + "%)");

		this.expBar.progress = this._curExp / curLvConf.NeedExp;
		this.expBarTx.string = this._curExp + '/' + curLvConf.NeedExp;
	}

	updateByExp() {
		if (Object.keys(this._selectEquip).length <= 0) {
			this.initUI()
		} else {
			let curLvConf = EquipManager.getEquipLevelByExp(this._curExp);
			let attr = this._curEquip.conf['Stat1'].split(':');
			let attrConf = AttrConf[attr[0]]
			let nowAdd = null;
			if (attrConf.type == AttrAddTypeEnum.reality) {
				nowAdd = attr[1] * (1 + curLvConf.AttackMod);
			} else {
				nowAdd = 100 * Number(attr[1]) * (1 + curLvConf.AttackMod);
			}
			let nextLvConf = EquipManager.getEquipLevelByExp(this._curExp + this._otherExp);
			if (nextLvConf.Id <= curLvConf.Id) {
				nextLvConf = EquipManager.getEquipNextLevel(this._curExp + this._otherExp);
			}
			this.equipNextLevel.string = 'Lv.' + nextLvConf.Id;
			let attrNextConf = AttrConf[attr[0]]
			let attrNextStr = StringUtil.getLanguageData(attrNextConf.name);
			let nextAdd = null;
			let valueNext = null;
			if (attrConf.type == AttrAddTypeEnum.reality) {
				nextAdd = attr[1] * (1 + nextLvConf.AttackMod);
				valueNext = " +" + parseFloat((attr[1] * (1 + nextLvConf.AttackMod)).toFixed(2));
			} else {
				nextAdd = 100 * Number(attr[1]) * (1 + nextLvConf.AttackMod);
				valueNext = " +" + parseFloat((100 * Number(attr[1]) * (1 + nextLvConf.AttackMod)).toFixed(2)) + "%";
			}
			attrNextStr = attrNextStr + valueNext;
			this.equipNextAttr.string = attrNextStr;

			this.equipNextAttrAdd.string = attrConf.type == AttrAddTypeEnum.reality ? (" (+" + parseFloat((nextAdd - nowAdd).toFixed(2)) + ")") :
				(" (+" + parseFloat((nextAdd - nowAdd).toFixed(2)) + "%)");

			this.expBar.progress = (this._curExp + this._otherExp) / curLvConf.NeedExp;
			this.expBarTx.string = (this._curExp + this._otherExp) + '/' + curLvConf.NeedExp;
		}
	}
}