import store from '../store'
import sensors from "sa-sdk-javascript";
import {
    addRecord
} from '../api/index'

import sdk from './buriedPoint'

const loginClick = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "发起登录",
        channelKey: channelKey,
        eventType: '7',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("clickLogin");
    }

};

const loginSuccess = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "登录成功",
        channelKey: channelKey,
        eventType: '8',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.login(phone)
        sensors.track("loginSuccess");
    }
    if (store.state.global.js === 0) {
        if (store.state.global.point === 0) {
            VuePageVisitor.setMp(phone);
        }
    }



};

const showSP = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "商品浏览",
        channelKey: channelKey,
        eventType: '0',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("lookSp", {
            attr4: product.spid,
            attr5: product.productName,
            attr6: product.originalPrice,
        });
    }
};

const buyClick = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "点击订购",
            channelKey: channelKey,
            eventType: '1',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("clickBuy", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice,
            });
        }
        if (store.state.global.js === 0) {
            if (store.state.global.point === 0) {
                for (let i = 0; i < sdk.length; i++) {
                    if (sdk[i].spid === product.spid) {
                        WoAnalytics.addPoint({
                            ad: sdk[i].appid,
                            ett: "点击",
                            ea: "点击订购"
                        })
                    }
                }
            }
        }
    }, 200)



}

const buySuccess = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "订购成功",
            channelKey: channelKey,
            eventType: '3',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("buySuccess", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice,
            });
        }
    }, 600)

}

const buyFail = (response) => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "订购失败",
        channelKey: channelKey,
        eventType: '4',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("buyFail", {
            attr4: product.spid,
            attr5: product.productName,
            attr6: product.originalPrice,
            attr7: response.message
        });
    }
}

const getToken = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "认证成功",
            channelKey: channelKey,
            eventType: '2',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("getToken", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice
            });
        }
    }, 400)

}

const unBuyClick = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "点击退订",
            channelKey: channelKey,
            eventType: '5',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("clickUnBuy", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice,
            });
        }
        if (store.state.global.js === 0) {
            if (store.state.global.point === 0) {
                for (let i = 0; i < sdk.length; i++) {
                    if (sdk[i].spid === product.spid) {
                        WoAnalytics.addPoint({
                            ad: sdk[i].appid,
                            ett: "点击",
                            ea: "点击退订"
                        })
                    }
                }
            }
        }
    }, 400)



}

const unBuySuccess = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData

    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "退订成功",
            channelKey: channelKey,
            eventType: '6',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("unBuySuccess", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice,
            });
        }
    }, 400)

}

const unBuyFail = (response) => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "退订失败",
            channelKey: channelKey,
            eventType: '13',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("unBuyFail", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice,
                attr7: response.message
            });
        }
    }, 400)

}

const memberClick = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "会员领取",
            channelKey: channelKey,
            eventType: '9',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("memberReceive", {
                attr4: product.productName,
                attr5: product.spid
            });
        }
        if (store.state.global.js === 0) {
            if (store.state.global.point === 0) {
                for (let i = 0; i < sdk.length; i++) {
                    if (sdk[i].spid === product.spid) {
                        WoAnalytics.addPoint({
                            ad: sdk[i].appid,
                            ett: "点击",
                            ea: "点击领取会员"
                        })
                    }
                }
            }
        }
    }, 400)

}

const memberSuccess = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "领取成功",
            channelKey: channelKey,
            eventType: '10',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("memberSuccess", {
                attr4: product.productName,
                attr5: product.spid
            });
        }
    }, 400)

}

const memberFail = (response) => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "领取失败",
            channelKey: channelKey,
            eventType: '11',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("memberFail", {
                attr4: product.productName,
                attr5: product.spid,
                attr6: response.message
            });
        }
    }, 400)

}

const clickBlindBox = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "点击盲盒",
        channelKey: channelKey,
        eventType: '12',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("clickBlindBox", {
            attr5: product.spid,
            attr4: product.productName,
        });
    }
}

const pageBrowseTime = () => {
    let time = localStorage.getItem('duration')
    let duration = time ? time : 0
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "页面浏览时长",
            channelKey: channelKey,
            eventType: '14',
            fromSource: fromSource,
            productId: product.id,
            duration: duration,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("pageStay", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice,
                attr7: duration,
            });
        }
        if (store.state.global.js === 0) {
            if (store.state.global.point === 0) {
                for (let i = 0; i < sdk.length; i++) {
                    if (sdk[i].spid === product.spid) {
                        WoAnalytics.addPoint({
                            ad: sdk[i].appid,
                            ett: "停留时常",
                            ea: "页面浏览时长(首页)",
                            dr: duration
                        })
                        WoAnalytics.addPoint({
                            ad: sdk[i].appid,
                            ett: "关闭",
                            ea: "pageClose"
                        })
                    }
                }
            }
        }
    }, 400)



}
const clickBanner = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "点击banner",
            channelKey: channelKey,
            eventType: '15',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("clickBanner", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice
            });
        }
    }, 400)

}
const explainBrowseTime = () => {
    let time = localStorage.getItem('ruleTime')
    let ruleTime = time ? time : 0
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData

    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "产品说明浏览时长",
            channelKey: channelKey,
            eventType: '16',
            fromSource: fromSource,
            productId: product.id,
            duration: ruleTime,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("explainStay", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice,
                attr7: ruleTime,
            });
        }
        if (store.state.global.js === 0) {
            if (store.state.global.point === 0) {
                for (let i = 0; i < sdk.length; i++) {
                    if (sdk[i].spid === product.spid) {
                        WoAnalytics.addPoint({
                            ad: sdk[i].appid,
                            ett: "停留时常",
                            ea: "页面浏览时长(产品说明页面)",
                            dr: ruleTime
                        })
                    }
                }
            }
        }
    }, 400)
}
const clickOrderRecord = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "点击订购记录",
        channelKey: channelKey,
        eventType: '17',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("searchOrderRecord", {
            attr4: product.spid,
            attr5: product.productName,
            attr6: product.originalPrice
        });
    }
}
const clcikReceive = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "点击领取话费",
            channelKey: channelKey,
            eventType: '18',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("clickGivecharge", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice
            });
        }
    }, 400)

}
const clickFollow = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    setTimeout(() => {
        addRecord({
            userid: userid,
            accessResource: "点击关注公众号",
            channelKey: channelKey,
            eventType: '19',
            fromSource: fromSource,
            productId: product.id,
            anonymousId: anonymousId,
            testId: testId,
            testGroupId: testGroupId,
            tid: tid
        });
        if (store.state.global.sensors === 0) {
            sensors.track("clickFollow", {
                attr4: product.spid,
                attr5: product.productName,
                attr6: product.originalPrice
            });
        }
    }, 400)

}
const clickRecommend = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "点击推荐",
        channelKey: channelKey,
        eventType: '20',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("clickRecommend", {
            attr4: product.spid,
            attr5: product.productName,
            attr6: product.originalPrice
        });
    }
}
const clickUnsubscribe = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "点击确定",
        channelKey: channelKey,
        eventType: '21',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("clickUnsubscribe", {
            attr4: product.spid,
            attr5: product.productName,
            attr6: product.originalPrice
        });
    }
}
const clickUnsubscribeGo = () => {
    let {
        channelKey,
        userid,
        phone,
        product,
        fromSource,
        testId,
        testGroupId,
        tid,
        anonymousId
    } = store.state.addrData
    addRecord({
        userid: userid,
        accessResource: "点击去看看",
        channelKey: channelKey,
        eventType: '22',
        fromSource: fromSource,
        productId: product.id,
        anonymousId: anonymousId,
        testId: testId,
        testGroupId: testGroupId,
        tid: tid
    });
    if (store.state.global.sensors === 0) {
        sensors.track("clickUnsubscribeGo", {
            attr4: product.spid,
            attr5: product.productName,
            attr6: product.originalPrice
        });
    }
}

//打开页面
const openPage = () => {
    let {
        phone,
        product,
    } = store.state.addrData
    if (store.state.global.js === 0) {
        if (store.state.global.point === 0) {
            if (phone) VuePageVisitor.setMp(phone);
            sdk.forEach(val => {
                if (product.spid === val.spid) {
                    WoAnalytics.addPoint({
                        ad: val.appid,
                        ett: "打开",
                        ea: "pageOpen"
                    }).then(res => {
                        localStorage.setItem('pageOpenTime', new Date())
                    })
                }
            })
        }
    }


}

export default {
    loginSuccess,
    showSP,
    loginClick,
    buyClick,
    buySuccess,
    buyFail,
    getToken,
    unBuyClick,
    unBuySuccess,
    unBuyFail,
    memberClick,
    memberSuccess,
    memberFail,
    clickBlindBox,
    pageBrowseTime,
    explainBrowseTime,
    clickBanner,
    clickOrderRecord,
    clcikReceive,
    clickFollow,
    clickRecommend,
    clickUnsubscribe,
    clickUnsubscribeGo,
    // closePage,
    openPage
}