package com.crm.manager.controller;

import com.crm.manager.service.UserGoogleEmailService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.user.google.UserGoogleEmailListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.user.google.UserGoogleEmailAddEntity;
import com.crm.rely.backend.core.entity.user.google.UserGoogleEmailDeleteEntitys;
import com.crm.rely.backend.core.entity.user.google.UserGoogleEmailSearchEntity;
import com.crm.rely.backend.core.entity.user.google.UserGoogleEmailUpdateEntity;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/google/email")
public class UserGoogleEmailController {

    @Autowired
    private UserGoogleEmailService userGoogleEmailService;

    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated UserGoogleEmailAddEntity entity) throws Exception {
        return BaseResultDto.success(Constants.SUCCESS,userGoogleEmailService.add(entity));
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated UserGoogleEmailDeleteEntitys entitys) throws Exception {

        userGoogleEmailService.delete(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated UserGoogleEmailUpdateEntity entity) throws Exception {
        userGoogleEmailService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/update/remark")
    public BaseResultDto updateRemark(@RequestBody @Validated UserGoogleEmailUpdateEntity entity) throws Exception {
        if(Strings.isNullOrEmpty(entity.getRemark())){
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        userGoogleEmailService.updateRemark(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<UserGoogleEmailListDto> pageList(@RequestBody UserGoogleEmailSearchEntity entity) throws Exception {

        return userGoogleEmailService.searchPageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        UserGoogleEmailListDto result = userGoogleEmailService.searchSingle(entity);
        return BaseResultDto.success(result);
    }

    @PostMapping("/refresh")
    public BaseResultDto refresh() throws Exception {
        userGoogleEmailService.refresh();
        return BaseResultDto.success();
    }

   /* @PostMapping("/add/googlemail")
    public BaseResultDto getGooglemail(@RequestBody UserEntryUpdateEntity entity) throws Exception {
        if (Strings.isNullOrEmpty(entity.getFirstName()) || Strings.isNullOrEmpty(entity.getLastName())) {
            return BaseResultDto.error(Constants.NAME_NOT_NULL);
        }
        return BaseResultDto.success(Constants.SUCCESS,userEntryService.getGooglemail(entity.getFirstName(),entity.getLastName(),entity.getEnglishName()));
    }*/

}
