package com.reactlibrary.uMeng;

import android.app.Application;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.common.inter.ITagManager;
import com.umeng.message.tag.TagManager;


import org.android.agoo.huawei.HuaWeiRegister;

import java.util.List;

import javax.annotation.Nonnull;

import androidx.annotation.NonNull;

public class UmPush extends ReactContextBaseJavaModule {


    private static ReactApplicationContext reactContext;
    public static UmPush instance;

    private static Application application;


    public static void register(Application app,String umKey,String channel,String pushSecret){
        UmPush.application = app;


        UMConfigure.setLogEnabled(true);
        UMConfigure.init(app, umKey, channel, UMConfigure.DEVICE_TYPE_PHONE, pushSecret);
//        该方法是【友盟+】Push后台进行日活统计及多维度推送的必调用方法，请务必调用！
        PushAgent.getInstance(app).onAppStart();
//        为了便于开发者更好的集成配置文件，我们提供了对于AndroidManifest配置文件的检查工具，可以自行检查开发者的配置问题。SDK默认是不检查集成配置文件的。自定义检查集成配置文件的接口如下
        PushAgent.getInstance(app).setPushCheck(true);


        PushAgent mPushAgent = PushAgent.getInstance(app);
        mPushAgent.setResourcePackageName("com.jzplus_rn");

        //获取消息推送代理示例
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
                Log.i("", "注册成功：deviceToken：-------->  " + deviceToken);
            }

            @Override
            public void onFailure(String s, String s1) {
                Log.e("", "注册失败：-------->  " + "s:" + s + ",s1:" + s1);
            }
        });

        HuaWeiRegister.register(app);
    }

    @NonNull
    @Override
    public String getName() {
        return "UmPush";
    }

    public UmPush(@Nonnull ReactApplicationContext context) {
        super(context);
        reactContext = context;
        instance = this;
    }


    /// 设置是否允许SDK当应用在前台运行收到Push时弹出Alert框（默认开启）
    /// @param value 是否开启弹出框
    @ReactMethod
    public void setAutoAlert(boolean value) {
        //获取消息推送代理示例
        PushAgent mPushAgent = PushAgent.getInstance(application);
        mPushAgent.setNotificaitonOnForeground(value);
    }

    /// 获取所有标签
    @ReactMethod
    public void getTags(final Promise callback) {
        PushAgent mPushAgent = PushAgent.getInstance(application);
        mPushAgent.getTagManager().getTags(new TagManager.TagListCallBack() {
            @Override
            public void onMessage(boolean isSuccess, List<String> list) {
                if (isSuccess) {
                    WritableArray array = Arguments.createArray();
                    for (String tag : list) {
                        array.pushString(tag);
                    }
                    callback.resolve(array);
                }else {
                    callback.reject("失败","失败");
                }
            }
        });
    }


    /// 添加标签
    /// @param tag 便签字符串
    @ReactMethod
    public void addTags(String tag,final Promise callback) {
        PushAgent mPushAgent = PushAgent.getInstance(application);
        mPushAgent.getTagManager().addTags(new TagManager.TCallBack() {
            @Override
            public void onMessage(boolean isSuccess, ITagManager.Result result) {
                if (isSuccess){
                    callback.resolve("成功");
                }else {
                    callback.reject("失败","失败");
                }
            }
        },tag);
    }


    /// 移除标签
    /// @param tag 便签字符串
    @ReactMethod
    public void deleteTags(String tag,final Promise callback) {
        PushAgent mPushAgent = PushAgent.getInstance(application);
        mPushAgent.getTagManager().deleteTags(new TagManager.TCallBack() {
            @Override
            public void onMessage(boolean isSuccess, ITagManager.Result result) {
                if (isSuccess){
                    callback.resolve("成功");
                }else {
                    callback.reject("失败","失败");
                }
            }
        },tag);
    }


}
