package cn.com.qmth.examcloud.core.questions.api;

import java.util.List;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import cn.com.qmth.examcloud.core.questions.dao.entity.dto.PropertyDto;
import cn.com.qmth.examcloud.core.questions.dao.entity.Property;
import cn.com.qmth.examcloud.core.questions.service.PropertyService;

/**
 * @author weiwenhai
 * @describle 属性 PropertyController
 * @date 2017.11.8
 */
@Controller
@RequestMapping("${api_cqb}/")
public class PropertyController extends ControllerSupport {

    @Autowired
    private PropertyService propertyService;

    @ApiOperation(value = "新增属性", notes = "新增属性")
    @PostMapping(value = "/property/save")
    public ResponseEntity<Object> saveProperty(@RequestBody Property property) {
        User user = getAccessUser();
        if (user != null) {
            try {
                propertyService.saveProperty(property, user);
                return new ResponseEntity<Object>(HttpStatus.OK);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "查询所有属性dto", notes = "树形结构")
    @GetMapping(value = "/property/all/{coursePropertyId}")
    public ResponseEntity<Object> findAllProperty(@PathVariable String coursePropertyId) {
        User user = getAccessUser();
        if (user == null) {
            return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
        }
        List<PropertyDto> propertyDtos = propertyService.propertyDtos(Long.parseLong(coursePropertyId), user);
        return new ResponseEntity<Object>(propertyDtos, HttpStatus.OK);
    }

    @ApiOperation(value = "更新属性", notes = "更新属性")
    @PutMapping(value = "/property/save")
    public ResponseEntity<Object> updateProperty(@RequestBody Property property) {
        User user = getAccessUser();
        if (user != null) {
            try {
                propertyService.saveProperty(property, user);
                return new ResponseEntity<Object>(HttpStatus.OK);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "删除属性", notes = "删除属性")
    @DeleteMapping(value = "/property/delete/{propertyId}/{coursePropertyId}")
    public ResponseEntity<Object> deleteProperty(@PathVariable Long propertyId, @PathVariable Long coursePropertyId) {
        User user = getAccessUser();
        if (user != null) {
            propertyService.deleteProperty(propertyId, coursePropertyId, user);
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "查询所有属性", notes = "查询所有属性")
    @GetMapping(value = "/property/{coursePropertyId}")
    public ResponseEntity<Object> findAll(@PathVariable String coursePropertyId) {
        User user = getAccessUser();
        if (user != null) {
            List<Property> properties = propertyService.findAll(Long.parseLong(coursePropertyId), user);
            return new ResponseEntity<Object>(properties, HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "根据一级查询二级所有属性", notes = "查询所有属性")
    @GetMapping(value = "/property/second/{propertyId}")
    public ResponseEntity<Object> findSons(@PathVariable Long propertyId) {
        User user = getAccessUser();
        if (user != null) {
            List<Property> properties = propertyService.findPropertySons(propertyId);
            return new ResponseEntity<Object>(properties, HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "根据课程属性id所有一级属性", notes = "查询所有属性")
    @GetMapping(value = "/property/first/{coursePropertyId}")
    public ResponseEntity<Object> findParents(@PathVariable Long coursePropertyId) {
        User user = getAccessUser();
        if (user != null) {
            List<Property> properties = propertyService.findPropertyParents(coursePropertyId, user.getRootOrgId());
            return new ResponseEntity<Object>(properties, HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "上移", notes = "上移")
    @PutMapping(value = "/property/moveUp")
    public ResponseEntity<Object> moveUp(@RequestBody Property property) {
        User user = getAccessUser();
        if (user != null) {
            try {
                property.setOrgId(user.getRootOrgId());
                propertyService.moveUp(property);
                return new ResponseEntity<Object>(HttpStatus.OK);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "下移", notes = "下移")
    @PutMapping(value = "/property/moveDown")
    public ResponseEntity<Object> moveDown(@RequestBody Property property) {
        User user = getAccessUser();
        if (user != null) {
            try {
                property.setOrgId(user.getRootOrgId());
                propertyService.moveDown(property);
                return new ResponseEntity<Object>(HttpStatus.OK);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }
}
