package com.adealink.weparty.im.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;

import com.adealink.frame.log.Log;
import com.adealink.weparty.commonui.util.BrandUtil;
import com.adealink.weparty.im.push.OfflinePushAPIDemo;
import com.adealink.weparty.im.push.OfflinePushConfigs;
import com.adealink.weparty.im.push.OfflinePushLocalReceiver;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMValueCallback;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuicore.TUIThemeManager;
import com.tencent.qcloud.tuicore.util.ErrorMessageConverter;
import com.tencent.qcloud.tuicore.util.PermissionRequester;
import com.tencent.qcloud.tuikit.tuichat.TUIChatConstants;
import com.tencent.qcloud.tuikit.tuichat.config.TUIChatConfigs;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class InitSetting {
    private static final String TAG = InitSetting.class.getSimpleName();
    public Context mContext;
    private OfflinePushAPIDemo offlinePushAPIDemo;
    private OfflinePushLocalReceiver offlinePushLocalReceiver = null;

    public InitSetting(Context context) {
        this.mContext = context;
    }

    public void init() {
        // add Demo theme
        setPermissionRequestContent();
        TUIChatConfigs.getGeneralConfig().setEnableMultiDeviceForCall(true);
        TUIChatConfigs.getGeneralConfig().setEnableVirtualBackgroundForCall(true);
        initOfflinePushConfigs();
    }

    public void setPermissionRequestContent() {

        // TODO: 2025/11/16 申请权限
        if (mContext == null) {
            return;
        }
        ApplicationInfo applicationInfo = mContext.getApplicationInfo();
        CharSequence labelCharSequence = applicationInfo.loadLabel(mContext.getPackageManager());
        String appName = "App";
        if (!TextUtils.isEmpty(labelCharSequence)) {
            appName = labelCharSequence.toString();
        }

//        PermissionRequester.PermissionRequestContent microphoneContent = new PermissionRequester.PermissionRequestContent();
//        microphoneContent.setReasonTitle(mContext.getResources().getString(R.string.demo_permission_mic_reason_title, appName));
//        String micReason = mContext.getResources().getString(R.string.demo_permission_mic_reason);
//        microphoneContent.setReason(micReason);
//        microphoneContent.setIconResId(R.drawable.demo_permission_icon_mic);
//        String micDeniedAlert = mContext.getResources().getString(R.string.demo_permission_mic_dialog_alert, appName);
//        microphoneContent.setDeniedAlert(micDeniedAlert);
//        PermissionRequester.setPermissionRequestContent(PermissionRequester.PermissionConstants.MICROPHONE, microphoneContent);
//
//        PermissionRequester.PermissionRequestContent cameraContent = new PermissionRequester.PermissionRequestContent();
//        cameraContent.setReasonTitle(mContext.getResources().getString(R.string.demo_permission_camera_reason_title, appName));
//        String cameraReason = mContext.getResources().getString(R.string.demo_permission_camera_reason);
//        cameraContent.setReason(cameraReason);
//        cameraContent.setIconResId(R.drawable.demo_permission_icon_camera);
//        String cameraDeniedAlert = mContext.getResources().getString(R.string.demo_permission_camera_dialog_alert, appName);
//        cameraContent.setDeniedAlert(cameraDeniedAlert);
//        PermissionRequester.setPermissionRequestContent(PermissionRequester.PermissionConstants.CAMERA, cameraContent);
    }

    private void initOfflinePushConfigs() {
        final SharedPreferences sharedPreferences = mContext.getSharedPreferences("TUIKIT_DEMO_SETTINGS", mContext.MODE_PRIVATE);
        int callbackMode = sharedPreferences.getInt("test_OfflinePushCallbackMode_v2", 1);
        Log.i(TAG, "initOfflinePushConfigs callbackMode = " + callbackMode);

        OfflinePushConfigs.getOfflinePushConfigs().setClickNotificationCallbackMode(callbackMode);

        // ring
        boolean enablePrivateRing = sharedPreferences.getBoolean("test_enable_private_ring", false);
        Map<String, Object> param = new HashMap<>();
        param.put(TUIChatConstants.OFFLINE_MESSAGE_PRIVATE_RING, enablePrivateRing);
        TUICore.notifyEvent(TUIChatConstants.EVENT_KEY_OFFLINE_MESSAGE_PRIVATE_RING, TUIChatConstants.EVENT_SUB_KEY_OFFLINE_MESSAGE_PRIVATE_RING, param);

        // register callback
        registerNotify();
    }

    // call after login success
    public void registerPushManually() {
        if (offlinePushAPIDemo == null) {
            offlinePushAPIDemo = new OfflinePushAPIDemo();
        }
        offlinePushAPIDemo.registerPush(mContext);
    }

    // call in Application onCreate
    private void registerNotify() {
        if (offlinePushAPIDemo == null) {
            offlinePushAPIDemo = new OfflinePushAPIDemo();
        }

        int callbackMode = OfflinePushConfigs.getOfflinePushConfigs().getClickNotificationCallbackMode();
        Log.d(TAG, "OfflinePush callback mode:" + callbackMode);
        switch (callbackMode) {
            case OfflinePushConfigs.CLICK_NOTIFICATION_CALLBACK_NOTIFY:
                // 1 TUICore NotifyEvent
                offlinePushAPIDemo.registerNotifyEvent();
                break;
            case OfflinePushConfigs.CLICK_NOTIFICATION_CALLBACK_BROADCAST:
                // 2 broadcast
                if (offlinePushLocalReceiver == null) {
                    offlinePushLocalReceiver = new OfflinePushLocalReceiver();
                }
                offlinePushAPIDemo.registerNotificationReceiver(mContext, offlinePushLocalReceiver);
                break;
            default:
                // 3 intent
                break;
        }
    }

    public void initBeforeLogin(int imsdkAppId) {
        Log.d(TAG, "initBeforeLogin sdkAppid = " + imsdkAppId);
        initBuildInformation();
    }

    private void initBuildInformation() {
        try {
            JSONObject buildInfoJson = new JSONObject();
            buildInfoJson.put("buildBrand", BrandUtil.getBuildBrand());
            buildInfoJson.put("buildManufacturer", BrandUtil.getBuildManufacturer());
            buildInfoJson.put("buildModel", BrandUtil.getBuildModel());
            buildInfoJson.put("buildVersionRelease", BrandUtil.getBuildVersionRelease());
            buildInfoJson.put("buildVersionSDKInt", BrandUtil.getBuildVersionSDKInt());
            
            // The Ministry of Industry and Information Technology requires the app to obtain device information only once
            // during its operation. Therefore, after the app obtains the device information and sets it to the SDK, the SDK
            // uses this value and no longer calls the system interface.
            V2TIMManager.getInstance().callExperimentalAPI("setBuildInfo", buildInfoJson.toString(), new V2TIMValueCallback<Object>() {
                @Override
                public void onSuccess(Object o) {
                    Log.i(TAG, "setBuildInfo success");

                }

                @Override
                public void onError(int code, String desc) {
                    Log.i(TAG, "setBuildInfo code:" + code + " desc:" + ErrorMessageConverter.convertIMError(code, desc));
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
