package com.fs.qw.controller;

import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.qw.domain.QwContact;
import com.fs.qw.domain.QwContactSearch;
import com.fs.qw.domain.QwConversation;
import com.fs.qw.domain.QwMessage;
import com.fs.qw.dto.AddContactDTO;
import com.fs.qw.dto.ModifyDTO;
import com.fs.qw.param.ConversationParam;
import com.fs.qw.param.CreateConParam;
import com.fs.qw.service.IQwContactService;
import com.fs.qw.service.IQwConversationService;
import com.fs.qw.service.IQwUserService;
import com.fs.qw.vo.QwContactListVO;
import com.fs.qw.vo.QwContactVO;
import com.fs.qw.vo.QwUserVO;
import com.fs.voice.utils.StringUtil;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("qw/contact")
public class QwContactController {

    @Autowired
    private IQwContactService contactService;

    @Autowired
    private IQwUserService qwUserService;

    @Autowired
    private IQwConversationService qwConversationService;

    @GetMapping("/search")
    public R search(@Param("deviceId") String deviceId,@Param("phone") Long phone){
        QwContactSearch qwContactSearch = contactService.search(deviceId,phone);
        return R.ok().put("data",qwContactSearch);
    }

    @PostMapping("/add")
    public R add(@RequestBody AddContactDTO addContactDTO){
        contactService.add(addContactDTO);
        return R.ok();
    }

    @GetMapping("/list/{deviceId}")
    public R list(@PathVariable("deviceId")String deviceId){
        QwUserVO qwUser = qwUserService.selectQwUserByDeviceId(deviceId);
        List<QwContactListVO> list = contactService.selectQwContactList(qwUser.getUserId().toString());
       return R.ok().put("data",list);
    }

    @GetMapping("/listByUser/{userId}")
    public R getListByUserId(@PathVariable("userId")Long userId){
        List<QwContactListVO> list = contactService.selectQwContactList(userId.toString());
        return R.ok().put("data",list);
    }




    @PreAuthorize("@ss.hasPermi('qw:contact:delete')")
    @PostMapping("/delete")
    public R delete( String deviceId,Long userId){
        contactService.delete(deviceId,userId);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('qw:contact:modify')")
    @PostMapping("/modify")
    public R modify(@RequestBody ModifyDTO modifyDTO){
        contactService.modify(modifyDTO);
        return R.ok();
    }

    @PostMapping("/detail")
    public R detail( String deviceId,Long userId){
        QwContactVO qwContact = contactService.detail(deviceId,userId);
        return R.ok().put("data",qwContact);
    }

    @PostMapping("/agreeFriend")
    public R agreeFriend(String deviceId,Long userId,Long corpId){
        contactService.agreeFriend(deviceId,userId,corpId);
        return R.ok();
    }

    //获取会话
    @GetMapping("/conversationList/{userId}")
    public R conversations(@PathVariable("userId")Long userId){
        List<QwContactListVO> list = qwConversationService.selectQwConversationByUserId(userId.toString());
        for (QwContactListVO contract:list) {
            if(StringUtils.isEmpty(contract.getDisplayName())){
                contract.setDisplayName("群聊");
            }
        }
        return R.ok().put("data",list);
    }

    //获取用户单条会话
    @GetMapping("/conversation")
    public R conversation(ConversationParam param){
        QwContactListVO data = qwConversationService.selectQwConversationBycId(param.getConversationId(),param.getUserId().toString());
        return R.ok().put("data",data);
    }

    //创建会话
    @PostMapping("/createConversation")
    private R createConversation(CreateConParam createConParam){

        return qwConversationService.createConversation(createConParam);
    }




}
