package com.sinosig.web.external.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.external.service.ORGUSERService;
import com.sinosig.web.external.domain.ORGUSER;
import com.sinosig.web.external.filter.*;

@RestController
@RequestMapping(value = "")
public class ORGUSERController {
	@Autowired
    ORGUSERService orguserservice;


	@RequestMapping(method = RequestMethod.POST, value = "/orgusers")
    public ResponseEntity<ORGUSER> create(@RequestBody ORGUSER orguser) {
        ORGUSER orguser2 = orguserservice.create(orguser);
        return ResponseEntity.status(HttpStatus.OK).body(orguser2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/orgusers/getdraft")
    public ResponseEntity<ORGUSER> getDraft() {
        //ORGUSER orguser = orguserservice.getDraft( orguser_id);
        return ResponseEntity.status(HttpStatus.OK).body(new ORGUSER());
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/orgusers/{orguser_id}")
    public ResponseEntity<ORGUSER> update(@PathVariable("orguser_id") String orguser_id, @RequestBody ORGUSER orguser) {
        ORGUSER orguser2 = orguserservice.update(orguser_id, orguser);
        return ResponseEntity.status(HttpStatus.OK).body(orguser2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/orgusers/{orguser_id}")
    public ResponseEntity<ORGUSER> get(@PathVariable("orguser_id") String orguser_id) {
        ORGUSER orguser = orguserservice.get( orguser_id);
        return ResponseEntity.status(HttpStatus.OK).body(orguser);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/orgusers/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody ORGUSER orguser) {
        boolean b = orguserservice.checkKey(orguser);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

	@RequestMapping(method = RequestMethod.POST, value = "/orgusers/{orguser_id}/save")
    public ResponseEntity<ORGUSER> save(@PathVariable("orguser_id") String orguser_id, @RequestBody ORGUSER orguser) {
        ORGUSER orguser2 = orguserservice.save(orguser_id, orguser);
        return ResponseEntity.status(HttpStatus.OK).body(orguser2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/orgusers/fetchcurorguser")
	public ResponseEntity<List<ORGUSER>> fetchCurOrgUser(ORGUSERSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<ORGUSER> page = orguserservice.fetchCurOrgUser(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());
    }

    @RequestMapping(method= RequestMethod.GET , value="/orgusers/fetchdefault")
	public ResponseEntity<List<ORGUSER>> fetchDefault(ORGUSERSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<ORGUSER> page = orguserservice.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());
    }


}
