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.UserHelper
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.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 UserHelper.getUsetSetting()
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    override fun getChatTeamHisShow(): Boolean {
        return UserHelper.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 !UserHelper.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 = UserHelper.getUsetSetting()?.lastVersionCode?:0
        val currentVersionCode : Int = RxAppTool.getAppVersionCode(BaseApp.getApp())
        if (lastVersionCode < currentVersionCode) {
            UserHelper.getUsetSetting()?.lastVersionCode = currentVersionCode
            return true
        }
        return false
    }

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