package com.app.controller.api;

import com.app.annotation.platform.OperationLog;
import com.app.dto.Result;
import com.app.dto.account.operation.WXMPLoginDto;
import com.app.service.guardian.GuardianChildService;
import com.app.service.guardian.GuardianLoginService;
import com.app.service.guardian.GuardianService;
import com.app.service.guardian.dto.GuardianDto;
import com.app.service.guardian.dto.WxmpLoginDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 功能描述：账号
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api("家长管理")
@Slf4j
@Validated
@RestController("ApiGuardianController")
@RequestMapping("/api/guardian")
public class GuardianController {

    @Autowired
    private GuardianService guardianService;

    @Autowired
    private GuardianLoginService guardianLoginService;

    @Autowired
    private GuardianChildService guardianChildService;

    @ApiOperation(value = "微信小程序登录")
    @PostMapping("/wxmp-login")
    public ResponseEntity<Result<WxmpLoginDto>> wxmpLogin(@RequestBody WXMPLoginDto WXMPLoginDto, HttpServletRequest request) {
        WxmpLoginDto dto;
        try {
            return new ResponseEntity(new Result().ok(guardianLoginService.wxMpLogin(request, WXMPLoginDto)), HttpStatus.OK);
        } catch (Exception e) {
            dto = new WxmpLoginDto();
            dto.setState(WxmpLoginDto.StateEnum.FAIL);
            dto.setMessage(WxmpLoginDto.StateEnum.FAIL.getName());
            return new ResponseEntity(new Result().serverError(dto), HttpStatus.OK);
        }
    }

    @ApiOperation(value = "账号信息")
    @PostMapping("/find")
    public ResponseEntity<Result<GuardianDto>> find() {
        String accountId = "";
        return new ResponseEntity(new Result().ok(guardianService.find2Dto(accountId)), HttpStatus.OK);
    }

    @OperationLog("更新 - 关联小孩")
    @ApiOperation(value = "关联小孩")
    @PostMapping("/child/relation")
    public ResponseEntity<Result> relation(@Valid @RequestBody GuardianChildRequest request) {
        String accountId = "";
        guardianChildService.relation(request, accountId);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    @OperationLog("删除 - 关联小孩")
    @ApiOperation(value = "删除关联小孩")
    @DeleteMapping("/child/{accountChildId}")
    public ResponseEntity<Result> deleteChild(@PathVariable("accountChildId") String accountChildId) {
        String accountId = "";
        guardianChildService.delete(accountId, accountChildId);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

}
