package com.casic.dcms.service;

import android.content.Context;
import android.os.Message;
import android.util.Log;

import com.casic.dcms.bean.NoticeLocaleBean;
import com.casic.dcms.utils.DataBaseManager;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.view.CaseDetailActivity;
import com.casic.dcms.view.MainActivity;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.LongKit;

/**
 * @author : Pengxh
 * @time : 2021/4/16 16:16
 * @email : 290677893@qq.com
 */
public class GeTuiIntentService extends GTIntentService {

    private static final String TAG = "GeTuiIntentService";

    @Override
    public void onReceiveServicePid(Context context, int i) {

    }

    // 处理透传消息
    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {

    }

    // 接收 cid
    @Override
    public void onReceiveClientId(Context context, String clientid) {
        //51ea9bde792e0697e1b3b6d7b48b6600
        Log.d(TAG, "onReceiveClientId -> " + clientid);
        //通知MainActivity注册个推服务
        Message msg = MainActivity.weakReferenceHandler.obtainMessage();
        msg.what = LocaleConstant.PUSH_REGISTER;
        msg.obj = clientid;
        MainActivity.weakReferenceHandler.sendMessage(msg);
    }

    // cid 离线上线通知
    @Override
    public void onReceiveOnlineState(Context context, boolean b) {

    }

    // 各种事件处理回执
    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {

    }

    // 通知到达，只有个推通道下发的通知会回调此方法
    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage msg) {
        /**
         * {
         *   "content": "工单1486662451409330177超时，请快速处理",
         *   "messageId": "15c1673cda4542f79c746d4dbfd9163b",
         *   "taskId": "TEST_0802_2ad46ce35d8b3bd9d8396de090ff6fc4",
         *   "title": "工单超时提醒",
         *   "appid": "rD5OSy72eI8v86VDdfdrO8",
         *   "clientId": "51ea9bde792e0697e1b3b6d7b48b6600",
         *   "pkgName": "com.casic.dcms"
         * }
         * */
        if (msg == null) {
            return;
        }
        NoticeLocaleBean noticeBean = new NoticeLocaleBean();
        noticeBean.setMessageId(msg.getMessageId());
        noticeBean.setUserId(StringHelper.getUserBean().getId());
        noticeBean.setUserAccount(StringHelper.getUserBean().getAccount());
        String msgContent = msg.getContent();
        String caseId = msgContent.substring(2, 21);
        noticeBean.setCasePrimaryKey(caseId);
        noticeBean.setTitle(msg.getTitle());
        noticeBean.setContent(msgContent);
        noticeBean.setPushDate(LongKit.timestampToCompleteDate(System.currentTimeMillis()));
        noticeBean.setIsRead("1");
        //存本地一份
        DataBaseManager.get().savePushNotice(noticeBean);
    }

    // 通知点击，只有个推通道下发的通知会回调此方法
    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage message) {
        String messageContent = message.getContent();
        String msgContent = message.getContent();
        String caseId = msgContent.substring(2, 21);
        ContextKit.navigatePageTo(context, CaseDetailActivity.class, caseId);
    }
}