import apiTools from '@/api'
export default {
    namespaced: true,
    state: {
        srvAdr: '', // 伺服器位置 China, Global
        srvAdrType: '', // 正式站 product 測試站 test
        China: {
            srvAdr: 'China',
            clientID: 'c7317f88-7cea-4e48-ac57-a16071f7b884',
            accAPIUrl: 'https://account.teammodel.cn',
            coreAPIUrl: 'https://api2.teammodel.cn',
            domainUrl: [
                {
                    station: 'product' ,
                    url: 'https://teammodelos.chinacloudsites.cn'
                },
                {
                    station: 'test' ,
                    url: 'https://teammodelos-test.chinacloudsites.cn'
                },
            ]
        },
        Global: {
            srvAdr: 'Global',
            clientID: '531fecd1-b1a5-469a-93ca-7984e1d392f2',
            accAPIUrl: 'https://account.teammodel.net',
            coreAPIUrl: 'https://api2.teammodel.net',
            domainUrl: [
                {
                    station: 'product' ,
                    url: 'https://www.teammodel.net'
                },
                {
                    station: 'test' ,
                    url: 'https://ies5-test.azurewebsites.net'
                },
            ]
        }
    },
    getters:{
        getSrvAdr: state => state.srvAdr,
    },
    mutations: {
        setSrvAdr(state, data) {
            state.srvAdr = data
        },
        setSrvAdrType(state, data) {
            state.srvAdrType = data
        },
    },
    actions: {
        checkSrvAdr(context) {
            let hostname = window.location.hostname
            let domainUrl = hostname

            let domainUrlArray = []

            context.state.China.domainUrl.forEach(function(item){
                item.srvAdr= 'China'
                domainUrlArray.push(item)
            })
            context.state.Global.domainUrl.forEach(function(item){
                item.srvAdr= 'Global'
                domainUrlArray.push(item)
            })

            let stationSetting = domainUrlArray.find(function(item){
                return item.url.indexOf(domainUrl) >= 0
            })

            if(!stationSetting){
                stationSetting = []
                stationSetting.station = 'localhost'
                stationSetting.srvAdr = 'China'

                if(localStorage.getItem('srvAdr')) stationSetting.srvAdr = localStorage.getItem('srvAdr')
            }

            context.commit('setSrvAdr', stationSetting.srvAdr)
            context.commit('setSrvAdrType', stationSetting.station)

            localStorage.setItem('srvAdr', stationSetting.srvAdr)
            localStorage.setItem('station', stationSetting.station)
            
        },
        setSrvAdr(context, param) {
            localStorage.setItem('srvAdr', param)
            context.commit('setSrvAdr', param)
        }
    }
}