package com.casic.dcms.service;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.casic.dcms.base.BaseApplication;
import com.casic.dcms.bean.NoticeBean;
import com.casic.dcms.bean.PushResultBean;
import com.casic.dcms.ui.CaseDetailActivity;
import com.casic.dcms.ui.MainActivity;
import com.casic.dcms.ui.fragment.NoticePageFragment;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.TimeOrDateUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;

/**
 * @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) {
        /**
         * appid = DR9oeLL9va6aG3DYXy2w39
         * taskid = TEST_0515_d4689edeff9808959f10365895682f6f
         * messageid = bd9c2465afd5464fb99368a579fd3fc8
         * pkg = com.casic.dcms
         * cid = 9e85018b1972d664d9e86e9a6d03b4a7
         * playload = {"content":"您有一个工单超时,请快速处理1","data":{"id":"1392655327805825025"},"id":1393472265320001538,"title":"工单超时提醒","type":"NORMAL"}
         */
        Gson gson = new Gson();
        String messageContent = new String(msg.getPayload());
        PushResultBean resultBean = gson.fromJson(messageContent, new TypeToken<PushResultBean>() {
        }.getType());
        NoticeBean noticeBean = new NoticeBean();
        noticeBean.setUserAccount(StringHelper.getUserAccount());
        noticeBean.setCaseId(resultBean.getData().getId());
        noticeBean.setTitle(resultBean.getTitle());
        noticeBean.setContent(resultBean.getContent());
        noticeBean.setType(resultBean.getType());
        noticeBean.setPushDate(TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()));
        noticeBean.setIsRead("1");

        //存本地一份
        BaseApplication.getDaoSession().getNoticeBeanDao().insert(noticeBean);

        //创建通知栏
        Message msgNotice = Message.obtain();
        msgNotice.what = Constant.PUSH_CREATE_NOTIFICATION;
        msgNotice.obj = noticeBean;
        MainActivity.sendMessage(msgNotice);

        //发送消息更新界面
        Message msgBean = Message.obtain();
        msgBean.what = Constant.PUSH_MESSAGE_DATA;
        msgBean.obj = noticeBean;
        NoticePageFragment.sendMessage(msgBean);
    }

    // 接收 cid
    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.d(TAG, "onReceiveClientId -> clientid = $clientid");
        //通知MainActivity注册个推服务
        if (!TextUtils.isEmpty(clientid)) {
            Message msg = Message.obtain();
            msg.what = Constant.PUSH_REGISTER;
            msg.obj = clientid;
            MainActivity.sendMessage(msg);
        } else {
            Log.d(TAG, "onReceiveClientId: ===>clientid = $clientid");
        }
    }

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

    }

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

    }

    // 通知到达，只有个推通道下发的通知会回调此方法
    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {
        Log.d(TAG, "onNotificationMessageArrived: 通知到达");
    }

    // 通知点击，只有个推通道下发的通知会回调此方法
    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage message) {
        Gson gson = new Gson();
        String messageContent = message.getContent();
        PushResultBean resultBean = gson.fromJson(messageContent, new TypeToken<PushResultBean>() {
        }.getType());
        String caseId = resultBean.getData().getId();
        Log.d(TAG, "onNotificationMessageClicked: " + caseId);
        Intent intent = new Intent(context, CaseDetailActivity.class);
        intent.putExtra("id", caseId);
        startActivity(intent);
    }
}