package com.yidianling.user.route

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.google.gson.Gson
import com.ydl.ydlcommon.base.BaseApp
import com.yidianling.common.tools.RxAppTool
import com.yidianling.router.user.IUserRouter
import com.yidianling.router.user.UserResponse
import com.yidianling.router.user.UserSetting
import com.yidianling.user.UserHelper
import com.yidianling.user.api.bean.UserResponseBean
import com.yidianling.user.api.bean.UserSettingBean
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
 */
class UserRouterImp : IUserRouter {
    val gson: Gson = Gson()

    override fun getUserSetting(): UserSetting {
        return gson.fromJson(UserHelper.getUserSettingStr(), UserSetting::class.java)
    }

    override fun updateUserInfoSp(userInfo: UserResponse.UserInfo?) {
        val info:UserResponseBean.UserInfo = gson.fromJson(gson.toJson(userInfo), UserResponseBean.UserInfo::class.java)
        UserHelper.updateUserinfo(info)
    }

    override fun updateUserSetingSp(userSetting: UserSetting?) {
        val info:UserSettingBean= gson.fromJson(gson.toJson(userSetting), UserSettingBean::class.java)
        UserHelper.updateUserSetting(info)
    }

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

    override fun setUserResponse(userInfo: UserResponse?) {
        val info:UserResponseBean = gson.fromJson(gson.toJson(userInfo), UserResponseBean::class.java)
        UserHelper.setUserinfo(info)
    }

    override fun getUserResponse(): UserResponse {
        return gson.fromJson(UserHelper.getUserInfoStr(), UserResponse::class.java)
    }

    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(): UserResponse.UserInfo? {
        return gson.fromJson(UserHelper.getUserInfoStr(), UserResponse::class.java)?.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()
    }
}