package com.sinosig.web.module.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import com.sinosig.web.module.service.DBDService;
import com.sinosig.web.module.domain.DBD;
import com.sinosig.web.module.filter.*;

@RestController
@RequestMapping(value = "")
public class DBDController {
	@Autowired
    DBDService dbdservice;


	@RequestMapping(method = RequestMethod.POST, value = "/dbds/{dbd_id}/save")
    public ResponseEntity<DBD> save(@PathVariable("dbd_id") String dbd_id, @RequestBody DBD dbd) {
        DBD dbd2 = dbdservice.save(dbd_id, dbd);
        return ResponseEntity.status(HttpStatus.OK).body(dbd2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/dbds/{dbd_id}")
    public ResponseEntity<DBD> update(@PathVariable("dbd_id") String dbd_id, @RequestBody DBD dbd) {
        DBD dbd2 = dbdservice.update(dbd_id, dbd);
        return ResponseEntity.status(HttpStatus.OK).body(dbd2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/dbds/getdraft")
    public ResponseEntity<DBD> getDraft() {
        //DBD dbd = dbdservice.getDraft( dbd_id);
        return ResponseEntity.status(HttpStatus.OK).body(new DBD());
    }

	@RequestMapping(method = RequestMethod.POST, value = "/dbds")
    public ResponseEntity<DBD> create(@RequestBody DBD dbd) {
        DBD dbd2 = dbdservice.create(dbd);
        return ResponseEntity.status(HttpStatus.OK).body(dbd2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/dbds/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody DBD dbd) {
        boolean b = dbdservice.checkKey(dbd);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.DELETE, value = "/dbds/{dbd_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("dbd_id") String dbd_id) {
        boolean b = dbdservice.remove( dbd_id);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/dbds/{dbd_id}")
    public ResponseEntity<DBD> get(@PathVariable("dbd_id") String dbd_id) {
        DBD dbd = dbdservice.get( dbd_id);
        return ResponseEntity.status(HttpStatus.OK).body(dbd);
    }

    @RequestMapping(method= RequestMethod.GET , value="/dbds/fetchdefault")
	public ResponseEntity<List<DBD>> fetchDefault(DBDSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<DBD> page = dbdservice.fetchDefault(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }


}
