import { _decorator, Component, Label, Node, tween, UIOpacity } from 'cc';
import { BaseView } from '../../../framework/layer/BaseView';
import { MailItem } from './MailItem';
import { StringUtil } from '../../../framework/util/StringUtil';
import { MailData } from '../../data/MailData';
import { GlobalConf } from '../../config/GlobalConf';
import List from '../../../framework/list/List';
import { Framework } from '../../../framework/Framework';
import { LoginMgr } from '../../common/LoginManager';
import { GameEvent } from '../../data/GameEvent';
import { MailManager } from '../../manager/MailManager';
const { ccclass, property } = _decorator;

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

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

	@property({ type: Label, tooltip: "邮件数文字" })
	numTx: Label = null;

	@property({ type: List, tooltip: "滑动容器" })
	sv: List = null;

	@property({ type: Node, tooltip: "为空提示节点" })
	noneNode: Node = null;

	@property({ type: Label, tooltip: "为空提示文字" })
	noneTx: Label = null;

	@property({ type: Node, tooltip: "一键删除按钮" })
	delAutoBtn: Node = null;
	@property({ type: Label, tooltip: "一键删除按钮文字" })
	delAutoBtnTx: Label = null;

	@property({ type: Node, tooltip: "一键领取按钮" })
	getAutoBtn: Node = null;
	@property({ type: Label, tooltip: "一键领取按钮文字" })
	getAutoBtnTx: Label = null;

	private _mailList = [];
	private _nowMailId: number = 0;

	onLoad() {
		super.onLoad();
		this.closeTips.string = StringUtil.getLanguageData('点击空白关闭');
		this.titlteTx.string = StringUtil.getLanguageData('邮件');
		this.noneTx.string = StringUtil.getLanguageData('当前空空如也哦~');
		this.delAutoBtnTx.string = StringUtil.getLanguageData('一键删除');
		this.getAutoBtnTx.string = StringUtil.getLanguageData('一键领取');
		this.closeTips.node.getComponent(UIOpacity).opacity = 0;
	}

	onDestroy() {
		super.onDestroy();
	}

	onOpen() {
		this.updateMainPanel();
		this.updateMailNum();
		Framework.event.addEvent(GameEvent.MailUpdate, () => {
			this.updateMainPanel();
			this.updateMailNum();
		}, this);

		tween(this.closeTips.node.getComponent(UIOpacity))
			.to(1, { opacity: 255 })
			.to(1.2, { opacity: 10 })
			.union()
			.repeatForever()
			.start()
	}

	onClose() {

	}

	onShow() {

	}

	onHide() {

	}

	private onTouchButton(event: Event, customStr) {
		let target: any = event.target;
		if (target.name == 'mask') {
			Framework.layer.close(this);

		} else if (target.name == 'auto_del_btn') {
			let ids = [];
			let list = MailManager.orderMail();
			for (const v of list) {
				if (Number(v.read) == 1) {
					ids.push(v.id);
				}
			}

			if (ids.length <= 0) {
				Framework.tips.setTips(StringUtil.getLanguageData('暂无可删邮件!'));
				return
			}
			MailManager.sendBatchDelMailMsg({ ids: ids },()=>{
				this.updateMainPanel();
				this.updateMailNum();
			});
		} else if (target.name == 'auto_get_btn') {
			let ids = [];
			let list = MailManager.orderMail();

			for (const v of list) {
				if (Number(v.read) == 0 && v.awards && v.awards.size > 0) {
					ids.push(v.id);
				}
			}
			if (ids.length <= 0) {
				Framework.tips.setTips(StringUtil.getLanguageData('暂无可领邮件!'));
				return
			}
			MailManager.sendBatchMaillAwardsMsg({ ids: ids },()=>{
				this.updateMainPanel();
				this.updateMailNum();
			});
		}
	}

	onEventList(item, idx) {
		item.getComponent(MailItem).refreshItem(this._mailList[idx]);
	}

	private updateMainPanel() {
		this._mailList = MailManager.orderMail();
		this._nowMailId = this._mailList.length > 0 ? this._mailList[0].id : 0;

		if (this._mailList.length == 0) {
			this.noneNode.active = true;
			this.delAutoBtn.active = false;
			this.getAutoBtn.active = false;
			this.numTx.node.active = false;
		} else {
			this.noneNode.active = false;
			this.delAutoBtn.active = true;
			this.getAutoBtn.active = true;
			this.numTx.node.active = true;
		}
		this.sv.numItems = this._mailList.length;
	}

	private updateMailNum() {
		this.numTx.string = `${StringUtil.getLanguageData('邮件数: ')}${this._mailList.length}/${GlobalConf.data.MailMaxCount.Value}`;
	}
}