package com.wecloud.rtc.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wecloud.im.entity.ImApplication;
import com.wecloud.im.entity.ImClient;
import com.wecloud.im.param.rtc.CandidateForwardParam;
import com.wecloud.im.param.rtc.CreateRtcChannelParam;
import com.wecloud.im.param.rtc.CreateRtcChannelResult;
import com.wecloud.im.param.rtc.JoinRtcChannelParam;
import com.wecloud.im.param.rtc.LeaveRtcChannelParam;
import com.wecloud.im.param.rtc.RejectRtcChannelParam;
import com.wecloud.im.param.rtc.SdpForwardParam;
import com.wecloud.im.service.ImApplicationService;
import com.wecloud.im.service.ImClientService;
import com.wecloud.im.ws.cache.UserStateListener;
import com.wecloud.im.ws.manager.ChannelManager;
import com.wecloud.rtc.entity.response.RtcCallResponse;
import com.wecloud.rtc.entity.response.RtcCandidateForwardResponse;
import com.wecloud.rtc.entity.response.RtcClientJoinResponse;
import com.wecloud.rtc.entity.response.RtcClientLeaveResponse;
import com.wecloud.rtc.entity.response.RtcClientRejectResponse;
import com.wecloud.rtc.entity.response.RtcSdpForwardResponse;
import com.wecloud.rtc.service.MangerRtcCacheService;
import com.wecloud.rtc.service.RtcService;
import com.wecloud.rtc.service.WsRtcWrite;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RtcServiceImpl extends UserStateListener implements RtcService {

    @Autowired
    private ImApplicationService imApplicationService;

    @Autowired
    private WsRtcWrite wsRtcWrite;

    /**
     * redis缓存
     */
    @Autowired
    private MangerRtcCacheService mangerRtcCacheService;

    @Autowired
    private ImClientService imClientService;

    @Autowired
    private ChannelManager channelManager;

    @Override
    public void onLineEvent(String appKey, String clientId, String longChannelId) {
        // nothing need to do
    }

    @Override
    public void offlineEvent(String appKey, String clientId, String longChannelId) {
        //  根据appKey查询appid
        ImApplication imApplication = imApplicationService.getOneByAppKey(appKey);
        ImClient client = imClientService.getCacheImClient(imApplication.getId(), clientId);

        // 获取该客户端加入的频道ID
        Long listByClientId = mangerRtcCacheService.getRtcChannelIdListByClientId(imApplication.getAppKey(), client.getClientId());
        if (listByClientId == null) {
            return;
        }

        LeaveRtcChannelParam leaveRtcChannelParam = new LeaveRtcChannelParam();
        leaveRtcChannelParam.setChannelId(listByClientId);
        // websocket离线逻辑  服务端踢出频道
        this.leave(leaveRtcChannelParam, client, imApplication);
    }

    @Override
    public ApiResult<CreateRtcChannelResult> createAndCall(CreateRtcChannelParam createRtcChannelParam) throws JsonProcessingException {
        ImClient client = imClientService.getCurentClient();
        Long rtcChannelId = SnowflakeUtil.getId();

        //  根据appKey查询appid
        ImApplication imApplication = imApplicationService.getById(client.getFkAppid());

        //  判断发起方必须在线
        boolean onlineStatus = channelManager.getOnlineStatus(imApplication.getAppKey(), client.getClientId());
        if (!onlineStatus) {
            log.info("发起方必须在线" + imApplication.getAppKey() + client.getClientId());
            ApiResult.fail();
        }

        // 添加缓存
        mangerRtcCacheService.create(imApplication.getAppKey(), client.getClientId(), rtcChannelId);

        CreateRtcChannelResult createRtcChannelResult = new CreateRtcChannelResult();
        createRtcChannelResult.setChannelId(rtcChannelId);

        // ws向接收方发送通知
        RtcCallResponse rtcCallResponse = new RtcCallResponse();
        rtcCallResponse.setType(createRtcChannelParam.getType());
//        rtcCallResponse.setConversationId(createRtcChannelParam.getConversationId());
        rtcCallResponse.setChannelId(rtcChannelId);
        rtcCallResponse.setClientId(client.getClientId());
        rtcCallResponse.setTimestamp(System.currentTimeMillis());
        wsRtcWrite.rtcCall(rtcCallResponse, imApplication.getAppKey(), createRtcChannelParam.getToClient());

        // TODO 待开发 下发安卓和ios系统推送

        return ApiResult.ok(createRtcChannelResult);


    }

    @Override
    public ApiResult<Boolean> join(JoinRtcChannelParam joinRtcChannelParam) {

        ImClient client = imClientService.getCurentClient();

        //  根据appKey查询appid
        ImApplication imApplication = imApplicationService.getById(client.getFkAppid());

        // 修改缓存
        mangerRtcCacheService.join(imApplication.getAppKey(), client.getClientId(), joinRtcChannelParam.getChannelId());

        //获取频道内所有client
        List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(joinRtcChannelParam.getChannelId());
        // 移除自己
        clientListByRtcChannelId.remove(client.getClientId());

        for (String toClientId : clientListByRtcChannelId) {

            // ws向接收方发送通知
            RtcClientJoinResponse rtcSdpForwardResponse = new RtcClientJoinResponse();

            rtcSdpForwardResponse.setChannelId(joinRtcChannelParam.getChannelId());
            rtcSdpForwardResponse.setClientId(client.getClientId());
            rtcSdpForwardResponse.setTimestamp(System.currentTimeMillis());
            wsRtcWrite.clientJoin(rtcSdpForwardResponse, imApplication.getAppKey(), toClientId);
        }

        return ApiResult.ok(true);
    }

    @Override
    public ApiResult<Boolean> reject(RejectRtcChannelParam rejectRtcChannelParam) {

        ImClient client = imClientService.getCurentClient();
        ImApplication imApplication = imApplicationService.getById(client.getFkAppid());

        //获取频道内所有client
        List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(rejectRtcChannelParam.getChannelId());
        // 移除自己
        clientListByRtcChannelId.remove(client.getClientId());


        for (String toClientId : clientListByRtcChannelId) {

            // ws向接收方发送通知
            RtcClientRejectResponse rtcClientRejectResponse = new RtcClientRejectResponse();

            rtcClientRejectResponse.setChannelId(rejectRtcChannelParam.getChannelId());
            rtcClientRejectResponse.setClientId(client.getClientId());
            rtcClientRejectResponse.setTimestamp(System.currentTimeMillis());
            wsRtcWrite.clientReject(rtcClientRejectResponse, imApplication.getAppKey(), toClientId);
        }
        return ApiResult.ok(true);
    }

    @Override
    public ApiResult<Boolean> leave(LeaveRtcChannelParam leaveRtcChannelParam) {

        ImClient client = imClientService.getCurentClient();

        //  根据appKey查询appid
        ImApplication imApplication = imApplicationService.getById(client.getFkAppid());
        this.leave(leaveRtcChannelParam, client, imApplication);

        return ApiResult.ok(true);
    }

    private void leave(LeaveRtcChannelParam leaveRtcChannelParam, ImClient client, ImApplication imApplication) {
        // 修改缓存
        mangerRtcCacheService.leave(imApplication.getAppKey(), client.getClientId(), leaveRtcChannelParam.getChannelId());

        //获取频道内所有client
        List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(leaveRtcChannelParam.getChannelId());
        // 移除自己
        clientListByRtcChannelId.remove(client.getClientId());

        for (String toClientId : clientListByRtcChannelId) {

            // ws向接收方发送通知
            RtcClientLeaveResponse rtcClientLeaveResponse = new RtcClientLeaveResponse();

            rtcClientLeaveResponse.setChannelId(leaveRtcChannelParam.getChannelId());
            rtcClientLeaveResponse.setClientId(client.getClientId());
            rtcClientLeaveResponse.setTimestamp(System.currentTimeMillis());
            wsRtcWrite.clientLeave(rtcClientLeaveResponse, imApplication.getAppKey(), toClientId);
        }

        // 判断频道内是否无其他人了
        if (mangerRtcCacheService.channelIsEmpty(leaveRtcChannelParam.getChannelId())) {
            // 移除频道信息
            mangerRtcCacheService.delChannelInfo(leaveRtcChannelParam.getChannelId());
        }
    }

    @Override
    public ApiResult<Boolean> sdpForward(SdpForwardParam sdpForwardParam) {

        ImClient client = imClientService.getCurentClient();
        Long rtcChannelId = SnowflakeUtil.getId();

        //  根据appKey查询appid
        ImApplication imApplication = imApplicationService.getById(client.getFkAppid());

        //  判断发起方必须在线
        boolean onlineStatus = channelManager.getOnlineStatus(imApplication.getAppKey(), client.getClientId());
        if (!onlineStatus) {
            log.info("发起方必须在线" + imApplication.getAppKey() + client.getClientId());
            ApiResult.fail();
        }

        CreateRtcChannelResult createRtcChannelResult = new CreateRtcChannelResult();
        createRtcChannelResult.setChannelId(rtcChannelId);

        //获取频道内所有client
        List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(sdpForwardParam.getChannelId());
        // 移除自己
        clientListByRtcChannelId.remove(client.getClientId());

        for (String toClientId : clientListByRtcChannelId) {

            // ws向接收方发送通知
            RtcSdpForwardResponse rtcSdpForwardResponse = new RtcSdpForwardResponse();
            rtcSdpForwardResponse.setSdpData(sdpForwardParam.getSdpData());
            rtcSdpForwardResponse.setSdpType(sdpForwardParam.getSdpType());

            rtcSdpForwardResponse.setChannelId(rtcChannelId);
            rtcSdpForwardResponse.setClientId(client.getClientId());
            rtcSdpForwardResponse.setTimestamp(System.currentTimeMillis());
            wsRtcWrite.sdpForward(rtcSdpForwardResponse, imApplication.getAppKey(), toClientId);
        }


        return ApiResult.ok(true);
    }

    @Override
    public ApiResult<Boolean> candidateForward(CandidateForwardParam candidateForwardParam) {

        ImClient client = imClientService.getCurentClient();
        Long rtcChannelId = SnowflakeUtil.getId();

        //  根据appKey查询appid
        ImApplication imApplication = imApplicationService.getById(client.getFkAppid());

        //  判断发起方必须在线
        boolean onlineStatus = channelManager.getOnlineStatus(imApplication.getAppKey(), client.getClientId());
        if (!onlineStatus) {
            log.info("发起方必须在线" + imApplication.getAppKey() + client.getClientId());
            ApiResult.fail();
        }

        CreateRtcChannelResult createRtcChannelResult = new CreateRtcChannelResult();
        createRtcChannelResult.setChannelId(rtcChannelId);

        //获取频道内所有client
        List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(candidateForwardParam.getChannelId());
        // 移除自己
        clientListByRtcChannelId.remove(client.getClientId());

        for (String toClientId : clientListByRtcChannelId) {

            // ws向接收方发送通知
            RtcCandidateForwardResponse rtcCandidateForwardResponse = new RtcCandidateForwardResponse();

            rtcCandidateForwardResponse.setCandidateData(candidateForwardParam.getCandidateData());

            rtcCandidateForwardResponse.setChannelId(rtcChannelId);
            rtcCandidateForwardResponse.setClientId(client.getClientId());
            rtcCandidateForwardResponse.setTimestamp(System.currentTimeMillis());
            wsRtcWrite.candidateForward(rtcCandidateForwardResponse, imApplication.getAppKey(), toClientId);
        }

        return ApiResult.ok(true);
    }
}
