<template>
    <div class="header">
        <img class="logo" src="../static/img/logo.svg" alt="DeNet" />
        <div class="operation">
            <!-- <div class="login" @click="twitterAuth">
                <img class="add" src="../static/img/header-add.svg" alt="">
                <span>Create NFTs</span>
            </div> -->
            <div class="down" @click="install">
                <div class="text">Install DeNet</div>
            </div>
        </div>
    </div>
    <div class="header-place"></div>
</template>

<script lang="ts" setup>
import Api from '../static/http/api'
import { postRequest } from '../static/http'
import { getMid, appVersionCode, getOauthUrl, createWindow, callBackUrl } from '../static/utils'
import { getStorage, removeStorage, setStorage, storageKey } from '../static/utils/storage'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const timer = ref(0)

const router = useRouter()

const install =  () => {
    window.open(`https://chrome.google.com/webstore/detail/denet/inlfbeejfdgkknpiodhemfcokbdgofja`);
}

const twitterAuth = () => {
    postRequest(Api.twitterRequestToken, {
        baseInfo: {
            mid: getMid(),
            appVersionCode,
        },
        params: {
            oauthCallback: callBackUrl
        }
    }).then(res => {
        let { code, data, msg }  = res;
        if ( code === 0 )  {
            let url = getOauthUrl(data.authToken);
            let win = createWindow(url);
            timer.value = setInterval(() => {
                if (win && win.closed) {
                    clearInterval(timer.value);
                    twitterLogin(data);
                }
            }, 500)
        } else {
            ElMessage({
                offset: 100,
                type: 'error',
                message: msg,
            })
        }
    })
}

const twitterLogin = (data: { authToken: string, consumerKey: string }) => {
    let verifier = getStorage(storageKey.verifier)
    if (verifier) {
        postRequest(Api.twitterLogin,  {
            baseInfo: {
                mid: getMid(),
                appVersionCode,
            },
            params: {
                consumerKey: data.consumerKey,
                oauthToken: data.authToken,
                oauthVerifier: verifier
            }
        }).then(res => {
            let { code, data } = res;
            if ( code === 0 ) {
                setStorage(storageKey.userInfo, data);
                removeStorage(storageKey.verifier);
                router.push(`/nft/list`)
            }
        })
    }
}
</script>

<style lang="less" scoped>
.header {
    position: fixed;
    z-index: 3;
    display: flex;
    justify-content: space-between;
    align-items: center;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    background-color: #ffffff;
    box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.1);
    .logo {
        height: 38px;
        margin-left: 26px;
    }
    .operation {
        display: flex;
        flex-direction: row;
    }
    .login {
        display: flex;
        align-items: center;
        justify-content: center;
        box-sizing: border-box;
        height: 38px;
        cursor: pointer;
        padding: 0 18px;
        font-size: 15px;
        color: #1D9BF0;
        margin-right: 19px;
        border-radius: 20px;
        border: solid 1px #1D9BF0;
        .add {
            width: 20px;
            margin-right: 4px;
        }
    }
    .down {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 38px;
        cursor: pointer;
        padding: 0 24px;
        margin-right: 26px;
        border-radius: 20px;
        background: #1D9BF0;
        .text {
            color: #fff;
            font-size: 15px;
            margin-top: -2px;
        }
    }
    &-place {
        height: 60px;
    }
}
</style>