package com.yidianling.user.modular_service

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.ydl.ydlcommon.base.BaseApp
import com.yidianling.common.tools.RxAppTool
import com.yidianling.user.api.bean.UserResponseBean
import com.yidianling.user.api.bean.UserSettingBean
import com.yidianling.user.api.service.IUserService
import com.yidianling.user.modular_service.utils.UserServiceHelper
import com.yidianling.user.route.UserIn
import com.yidianling.user.safePrivate.FingerPrintUtil
import com.yidianling.user.safePrivate.PrivacyActivity
import com.yidianling.user.ui.InputPhoneActivity
import com.yidianling.user.ui.login.RegisterAndLoginActivity

/**
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/04/13
 */
@Route(path = "/user/UserService")
public class UserServiceImp : IUserService {

    constructor()

    override fun init(context: Context?) {

    }

    override fun getUserSetting(): UserSettingBean? {
        return UserServiceHelper.getUsetSetting()
    }

    override fun updateUserInfoSp(userInfo: UserResponseBean.UserInfo?) {
        UserServiceHelper.updateUserinfo(userInfo)
    }

    override fun updateUserSetingSp(userSetting: UserSettingBean?) {
        UserServiceHelper.updateUserSetting(userSetting)
    }

    override fun setChatTeamHisShowed(showed: Boolean) {
        UserServiceHelper.getUsetSetting()?.chatTeamHisShowed = showed
    }

    override fun setUserResponse(userInfo: UserResponseBean?) {
        UserServiceHelper.setUserinfo(userInfo)
    }

    override fun getUserResponse(): UserResponseBean? {
        return UserServiceHelper.getUserInfo()
    }

    override fun isLogin(): Boolean {
        return UserServiceHelper.isLogin()
    }

    override fun isFirstLogin(): Boolean {
        return UserServiceHelper.getUserInfo()?.firstLogin==1
    }

    override fun setFirstLogin(first: Boolean) {
        var value : Int = if (first) 1 else 2 //1是2否
        UserServiceHelper.getUserInfo()?.firstLogin = value
    }

    override fun isSafePrivacyClicked(): Boolean {
        return UserServiceHelper.getUsetSetting()?.meSafePrivateIsClick?:true
    }

    override fun putSafePrivacyClicked(clicked: Boolean) {
        UserServiceHelper.getUsetSetting()?.meSafePrivateIsClick = clicked
    }

    override fun getUserInfo(): UserResponseBean.UserInfo? {
        return UserServiceHelper.getUserInfo()?.userInfo
    }

    override fun isBindPhone(): Boolean {
        return !TextUtils.isEmpty(UserServiceHelper.getUserInfo()?.userInfo?.phone)
    }

    override fun putUnlockCheckSuccessTime(time: Long) {
        UserServiceHelper.getUsetSetting()?.unLockCheckSuccessTime = time
    }

    override fun getChatTeamHisShow(): Boolean {
        return UserServiceHelper.getUsetSetting()?.chatTeamHisShowed?:true
    }

    override fun privacyIntent(activity: Activity): Intent {
        return Intent(activity, PrivacyActivity::class.java)
    }

    override fun loginWayIntent(context: Context): Intent {
        return RegisterAndLoginActivity.getIntent(context)
    }

    override fun inputPhoneIntent(activity: Activity, smsAction: String): Intent {
        return InputPhoneActivity.newIntent(activity, smsAction)
    }

    override fun safeTipViewGone(): Boolean {
        //如果是非登录状态 或者 app指纹开启 或者 手势密码不为空 或者 已提示过安全解锁 -> 隐藏 设置密码提示
        return !UserServiceHelper.isLogin() || FingerPrintUtil.instance().fingerPrintIsOpen()
                || !TextUtils.isEmpty(FingerPrintUtil.instance().getHandPass())
                || FingerPrintUtil.instance().getTrendsSafeTip()
    }

    override fun setTrendsSafeTip(status: Boolean) {
        FingerPrintUtil.instance().setTrendsSafeTip(status)
    }

    override fun errorAgainTime(): Long {
        return FingerPrintUtil.errorAgainTime
    }

    override fun isFirstStart(): Boolean {
        val lastVersionCode : Int = UserServiceHelper.getUsetSetting()?.lastVersionCode?:0
        val currentVersionCode : Int = RxAppTool.getAppVersionCode(BaseApp.getApp())
        if (lastVersionCode < currentVersionCode) {
            UserServiceHelper.getUsetSetting()?.lastVersionCode = currentVersionCode
            return true
        }
        return false
    }

    override fun clearUserInfo() {
        UserServiceHelper.setUserinfo(null)
        UserIn.clearImData()
    }
}