package com.adealink.weparty.im.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.adealink.frame.log.Log;
import com.adealink.weparty.commonui.toast.util.ToastUtilKt;
import com.adealink.weparty.im.config.InitSetting;
import com.adealink.weparty.im.data.UserInfo;
import com.google.auto.service.AutoService;
import com.tencent.qcloud.tuicore.ServiceInitializer;
import com.tencent.qcloud.tuicore.TUILogin;
import com.tencent.qcloud.tuicore.annotations.TUIInitializerDependency;
import com.tencent.qcloud.tuicore.annotations.TUIInitializerID;
import com.tencent.qcloud.tuicore.interfaces.ITUIService;
import com.tencent.qcloud.tuicore.interfaces.TUICallback;
import com.tencent.qcloud.tuicore.interfaces.TUIInitializer;
import com.tencent.qcloud.tuicore.interfaces.TUILoginListener;

@AutoService(TUIInitializer.class)
@TUIInitializerDependency("TIMCommon")
@TUIInitializerID("TIMAppService")
public class TIMAppService implements TUIInitializer, ITUIService {
    public static final String TAG = TIMAppService.class.getSimpleName();
    private static TIMAppService instance;

    public static TIMAppService getInstance() {
        return instance;
    }

    public Context mContext;

    private BroadcastReceiver languageChangedReceiver;
    private InitSetting initSetting;

    @Override
    public void init(Context context) {
        instance = this;
        mContext = context;

        initSetting = new InitSetting(mContext);
        initSetting.init();
        initThemeAndLanguageChangedReceiver();
        initLoginStatusListener();
    }

    public void initLoginStatusListener() {
        TUILogin.addLoginListener(loginStatusListener);
    }

    private final TUILoginListener loginStatusListener = new TUILoginListener() {
        @Override
        public void onKickedOffline() {
            // TODO: 2025/11/16 测试
            ToastUtilKt.showToast("测试: 您的账号已在其它终端登录");
            logout();
        }

        @Override
        public void onUserSigExpired() {
            // TODO: 2025/11/16 触发重新登录
            ToastUtilKt.showToast("测试: 账号已过期，请重新登录");
            TUILogin.logout(new TUICallback() {
                @Override
                public void onSuccess() {
                    logout();
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                    logout();
                }
            });
        }
    };

    public void logout() {
        Log.i(TAG, "logout");
        UserInfo.getLoginUserInfo().cleanUserInfo();
        // TODO: 2025/11/16 登出触发重新登录
    }

    private void initThemeAndLanguageChangedReceiver() {
        languageChangedReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                initSetting.setPermissionRequestContent();
            }
        };
    }

    public void registerPushManually() {
        if (initSetting == null) {
            initSetting = new InitSetting(mContext);
        }

        initSetting.registerPushManually();
    }

    public void initBeforeLogin(int sdkappid) {
        if (initSetting == null) {
            initSetting = new InitSetting(mContext);
        }

        initSetting.initBeforeLogin(sdkappid);
    }

    public static Context getAppContext() {
        return ServiceInitializer.getAppContext();
    }
}
