<template>
	<div class="page_container">
		<NavBar :title="$t('friendSettings')" />

		<div class=" overflow-hidden ">
			<CardDescItem :lable="$t('setNickname')" arrow @click="toChangeName" />
			<CardDescItem :lable="$t('shareFriend')" arrow @click="toShareCard" />
		</div>

		<div class=" mb-2 my-2  overflow-hidden ">
			<CardDescItem :lable="$t('checks.addToBlack')" arrow>
				<van-switch size="20" :loading="toggleBlackLoading" :model-value="comptIsBlack"
					@update:model-value="toggleBlack" />
			</CardDescItem>
		</div>

		<div class=" ">
			<van-button class="w-full !border-0 !text-base !text-error-text" plain type="default" :text="$t('unfriend')"
				@click="tryRemoveFriend" />
		</div>
	</div>
</template>

<script setup lang="ts">
	import NavBar from '@/components/NavBar/index.vue'
	import CardDescItem from '@/components/CardDescItem/index.vue'
	import useContactStore from '@/store/modules/contact'
	import { IMSDK } from '@/utils/imCommon'
	import { feedbackToast } from '@/utils/common'
	import { showConfirmDialog } from 'vant'
	import { ContactChooseEnum } from '../chooseUser/data'
	import useConversationStore from '@/store/modules/conversation'
	const conversationStore = useConversationStore()
	const { t } = useI18n()
	const router = useRouter()
	const contactStore = useContactStore()

	const comptIsBlack = computed(
		() =>
			contactStore.storeBlackList.findIndex(
				(user) => user.userID === contactStore.storeUserCardData.baseInfo?.userID,
			) > -1,
	)
	const toggleBlackLoading = ref(false)
	const toggleBlack = (newValue : boolean) => {
		toggleBlackLoading.value = true
		if (newValue) {
			IMSDK.deleteConversationAndDeleteAllMsg(conversationStore.storeCurrentConversation.conversationID)
				.then(() => conversationStore.delConversationByCID(conversationStore.storeCurrentConversation.conversationID))
				.catch((error) => feedbackToast({ error }))
			IMSDK.addBlack({
				toUserID: contactStore.storeUserCardData.baseInfo?.userID!,
				ex: "",
			})
				.catch((error) => feedbackToast({ error }))
				.finally(() => (toggleBlackLoading.value = false));
			IMSDK.deleteFriend(contactStore.storeUserCardData.baseInfo?.userID!)
				.then(() => router.push('/conversation'))
				.catch((error) => feedbackToast({ error }))
				.finally()
			return;
		}
		IMSDK.removeBlack(contactStore.storeUserCardData.baseInfo?.userID!)
			.catch((error) => feedbackToast({ error }))
			.finally(() => (toggleBlackLoading.value = false));
	}

	const toChangeName = () => {
		router.push({
			path: 'changeNameOrRemark',
			query: {
				friendInfo: JSON.stringify(contactStore.storeUserCardData.baseInfo),
			},
		})
	}

	const toShareCard = () => {
		router.push({
			path: 'chooseUser',
			state: {
				chooseType: ContactChooseEnum.ShareCard,
				extraData: JSON.stringify(contactStore.storeUserCardData.baseInfo),
			},
		})
	}

	const tryRemoveFriend = () => {
		showConfirmDialog({
			message: t('messageTip.unfriend'),
			beforeClose: (action) =>
				new Promise((resolve) => {
					if (action === 'confirm') {
						IMSDK.deleteConversationAndDeleteAllMsg(conversationStore.storeCurrentConversation.conversationID)
							.then(() => conversationStore.delConversationByCID(conversationStore.storeCurrentConversation.conversationID))
							.catch((error) => feedbackToast({ error }))
						IMSDK.deleteFriend(contactStore.storeUserCardData.baseInfo?.userID!)
							.then(() => router.back())
							.catch((error) => feedbackToast({ error }))
							.finally(() => resolve(true))
					} else {
						resolve(true)
					}
				}),
		})
	}
</script>

<style lang="scss" scoped></style>