<template>
	<div class="mobile-land-page">
		<div class="mobile-land-page-invited-info" v-if="useFul && !isNFTCpd">
			<img :src="userInfo.avatarUrl" class="invited-photo" />
			<div class="invited-name">{{ userInfo.nickName }}</div>
			<div class="invited-text">{{ isTreasureCpd ? 'Invite You to Hunt the Treasure' : 'Send You Giveaway!' }}</div>
		</div>
		<div class="mobile-land-page-icon-wrap">
			<img class="mobile-land-page-icon" :src="iconCpd" />
		</div>
		<div class="mobile-land-page-prize-info">
			<template v-if="!useFul">
				<div class="mobile-land-page-prize-info-test">
					<!-- time-expired -->
				</div>
			</template>
			<!-- 红包 -->
			<template v-else-if="isCommonCpd">
				<div class="mobile-land-page-prize-info-test">You are gifted an entries to earn</div>
				<div class="mobile-land-page-prize-info-test">
					<img v-if="!isCustomRewardCpd" class="icon" :src="currencyIconPath" />
					<span class="pre-amount-value">{{ isCustomRewardCpd ? 1 : amountValue }}</span>
					<span class="prize-name">{{ prize }}</span>
					<span class="usd-amount" v-if="!isCustomRewardCpd"> (${{ usValue }})</span>
				</div>
			</template>
			<!-- 抽奖 -->
			<template v-else-if="isLottaryCpd">
				<div class="mobile-land-page-prize-info-test">
					You Won
					<img v-if="!isCustomRewardCpd" class="icon" :src="currencyIconPath" />
					<span class="pre-amount-value">{{ isCustomRewardCpd ? 1 : amountValue }}</span>
					<FontZoom width="240" style="color: #000">
						<span class="prize-name">{{ prize }}</span>
					</FontZoom>
					<span class="usd-amount" v-if="!isCustomRewardCpd"> (${{ usValue }})</span>
				</div>
				<div class="mobile-land-page-prize-info-test">in the Giveaway!</div>
			</template>
			<!-- 夺宝 -->
			<template v-else-if="isTreasureCpd">
				<div class="mobile-land-page-prize-info-test">Complete the quest to win</div>
				<div class="mobile-land-page-prize-info-test">
					<img class="icon" :src="currencyIconPath" />
					<span class="pre-amount-value">{{ amountValue }}</span>
					<span class="prize-name">{{ prize }}</span>
					<span class="usd-amount"> (${{ usValue }})</span>
					for you two!
				</div>
			</template>
			<!-- NFT -->
			<template v-else-if="isNFTCpd">
				<div class="mobile-land-page-prize-info-test">
					<span class="prize-name">{{ prize }}</span>
					is in your
				</div>
				<div class="mobile-land-page-prize-info-test">DeNet NFT wallet!</div>
			</template>
			<!-- 兜底显示 -->
			<template v-else>
				<div class="mobile-land-page-prize-info-test"></div>
			</template>
		</div>
		<FontZoom width="330">
			<div class="mobile-land-page-tip">{{ tipCpd }}</div>
		</FontZoom>
		<div class="mobile-land-page-login-twitter" @click="toLogin">{{ isLoginCpd || loginSuccessBack ? 'Claim Prize' : 'Login Twitter' }}</div>
	</div>
</template>
<script>
import { RewardType, PlayType } from '../types';
import { getStorage, setStorage, removeStorage, storageKey, getOauthUrl } from '../utils/help';
import { postRequest } from '../http';
import FontZoom from './FontZoom';
import { Toast } from 'vant';

const overTimePic = require('../static/img/icon-h5-denet.svg');
const giveawayPic = require('../static/img/icon-h5-giveaway.svg');
const redpackPic = require('../static/img/icon-h5-redpack.svg');
const treasurePic = require('../static/img/icon-h5-treasure.svg');

export default {
	name: 'mobileLandPage',
	props: {
		playType: {
			// 玩法类型, 红包 / 抽奖 / 夺宝 / NFT
			type: Number,
		},
		rewardType: {
			// 奖品类型  货币/ 自定义奖品
			type: Number,
			default: 1,
		},
		useFul: {
			// 红包仍在有效期，可领取
			type: Boolean,
			default: true,
		},
		userInfo: {
			type: Object,
			default: () => {
				return {};
			},
		},
		currencyIconPath: {
			// 货币头像
			type: String,
			default: '',
		},
		amountValue: {
			// 奖品数量
			type: String,
			default: '1',
		},
		usValue: {
			// 转换为美金 的价值
			type: String,
			default: '',
		},
		prize: {
			// 奖品 描述
			type: String,
			default: '',
		},
		postId: {
			// 推文ID，用于retweer
			type: String,
			default: '',
		},
		prizePicPath: {
			// NFT图片
			type: String,
			default: '',
		},
	},
	data() {
		return {
			timer: {},
			loginSuccessBack: false,
		};
	},
	computed: {
		isCustomRewardCpd() {
			return this.rewardType === RewardType.custom;
		},
		isLottaryCpd() {
			return this.playType === PlayType.lottery;
		},
		isCommonCpd() {
			return this.playType === PlayType.common;
		},
		isTreasureCpd() {
			return this.playType === PlayType.Treasure;
		},
		isNFTCpd() {
			return this.playType === PlayType.NFT;
		},
		iconCpd() {
			if (!this.useFul) {
				return overTimePic;
			} else if (this.isCommonCpd) {
				return giveawayPic;
			} else if (this.isLottaryCpd) {
				return redpackPic;
			} else if (this.isTreasureCpd) {
				return treasurePic;
			} else if (this.isNFTCpd) {
				return this.prizePicPath;
			}
			{
				return overTimePic;
			}
		},
		tipCpd() {
			if (this.isCommonCpd) {
				return 'to claim,  log in twitter to install Denet Chrome Extension';
			} else if (this.isLottaryCpd) {
				return 'To participate, login Twitter to install DeNet Chrome Extension';
			} else if (this.isTreasureCpd) {
				return 'To complete, log in twitter to install Denet Chrome Extension';
			} else {
				return 'login Twitter to install DeNet Chrome Extension';
			}
		},
		isLoginCpd() {
			return !!getStorage(storageKey.userInfo);
		},
	},
	methods: {
		toLogin() {
			let userInfo = getStorage(storageKey.userInfo);
			if (userInfo) {
				location.href = `/course?useful=${this.useFul ? '1' : '0'}&playType=${this.playType}&rewardType=${this.rewardType}&postId=${this.postId}`;
			} else {
				this.twitterAuth();
			}
		},
		cancelLogin() {
			location.href = `/course?useful=${this.useFul ? '1' : '0'}&playType=${this.playType}&rewardType=${this.rewardType}&postId=${this.postId}`;
		},
		async twitterAuth() {
			let win = window.open();
			win.opener = null;
			postRequest(`/denet/user/twitterRequestToken`, {
				params: {
					oauthCallback: `${location.protocol}//${location.host}/authlogin`,
				},
			}).then(({ code, data }) => {
				if (code == 0) {
					let url = getOauthUrl(data.authToken);
					win.location.href = url;
					this.timer.value = setInterval(() => {
						if (win && win.closed) {
							clearInterval(this.timer.value);
							this.twitterLogin(data);
							if (getStorage(storageKey.backFromTwitterLogin)) {
								removeStorage(storageKey.backFromTwitterLogin);
								this.cancelLogin();
							}
						}
					}, 500);
				} else {
					Toast('login fail');
					win.close();
					this.cancelLogin();
				}
			});
		},
		async twitterLogin(authData) {
			let verifier = getStorage(storageKey.verifier);
			if (verifier) {
				postRequest(`/denet/user/twitterLogin`, {
					params: {
						consumerKey: authData.consumerKey,
						oauthToken: authData.authToken,
						oauthVerifier: verifier,
					},
				}).then(({ code, data }) => {
					if (code == 0) {
						setStorage(storageKey.userInfo, data);
						removeStorage(storageKey.verifier);
						this.loginSuccessBack = true;
						location.href = `/course?useful=${this.useFul ? '1' : '0'}&playType=${this.playType}&rewardType=${this.rewardType}&postId=${this.postId}`;
					}
				});
			}
		},
	},
	unmounted() {
		removeStorage(storageKey.backFromTwitterLogin);
	},
	components: { FontZoom },
};
</script>
<style lang="scss" scoped>
.mobile-land-page {
	min-height: 100%;
	max-height: 100%;
	background: linear-gradient(180deg, #cceaff 0%, #ffffff 70.42%);
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	align-items: center;
	padding: 0 16px;
	&-invited-info {
		width: 302px;
		height: 72px;
		margin-top: 28px;
		position: relative;
		padding-left: 81px;
		border-radius: 35px;
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: flex-start;
		background: #ffffff;
		border: 1px solid #c0daeb;
		.invited-photo {
			width: 70px;
			height: 70px;
			border-radius: 50%;
			position: absolute;
			left: 0;
			top: 0;
		}
		.invited-name {
			margin: 13px 0 5px;
			font-weight: 500;
			font-size: 13px;
			line-height: 16px;
			letter-spacing: 0.3px;
			color: #000000;
			max-width: 210px;
			text-overflow: ellipsis;
			white-space: nowrap;
			overflow: hidden;
		}
		.invited-text {
			font-weight: 700;
			font-size: 17px;
			line-height: 20px;
			letter-spacing: 0.3px;
			color: #f99d23;
		}
	}
	&-icon-wrap {
		display: flex;
		flex: 1;
		align-items: center;
		.mobile-land-page-icon {
			width: 100%;
		}
	}
	&-prize-info {
		font-style: normal;
		font-weight: 700;
		font-size: 18px;
		line-height: 21px;
		color: #000000;
		&-test {
			display: flex;
			align-items: center;
			justify-content: center;
			.pre-amount-value {
				margin-left: 3px;
			}
			.icon {
				width: 22px;
				height: 22px;
				margin-left: 3px;
			}
			.prize-name {
				margin: 0 3px;
			}
		}
	}
	&-tip {
		font-weight: 400;
		font-size: 13px;
		line-height: 16px;
		text-align: center;
		color: #7b7b7b;
		margin-top: 10px;
	}
	&-login-twitter {
		width: 100%;
		height: 54px;
		margin: 18px 16px 30px;
		border-radius: 54px;
		background: #1d9bf0;
		text-align: center;
		line-height: 54px;
		font-weight: 700;
		font-size: 18px;
		text-align: center;
		color: #fff;
	}
}
</style>
