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.ORGService;
import com.sinosig.web.external.domain.ORG;
import com.sinosig.web.external.filter.*;

@RestController
@RequestMapping(value = "")
public class ORGController {
	@Autowired
    ORGService orgservice;


    @RequestMapping(method= RequestMethod.GET , value="/orgs/fetchtoporg")
	public ResponseEntity<List<ORG>> fetchTopOrg(ORGSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<ORG> page = orgservice.fetchTopOrg(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="/orgs/fetchcurorg")
	public ResponseEntity<List<ORG>> fetchCurOrg(ORGSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<ORG> page = orgservice.fetchCurOrg(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.POST, value = "/orgs/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody ORG org) {
        boolean b = orgservice.checkKey(org);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/orgs")
    public ResponseEntity<ORG> create(@RequestBody ORG org) {
        ORG org2 = orgservice.create(org);
        return ResponseEntity.status(HttpStatus.OK).body(org2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/orgs/{org_id}")
    public ResponseEntity<ORG> get(@PathVariable("org_id") String org_id) {
        ORG org = orgservice.get( org_id);
        return ResponseEntity.status(HttpStatus.OK).body(org);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/orgs/{org_id}/save")
    public ResponseEntity<ORG> save(@PathVariable("org_id") String org_id, @RequestBody ORG org) {
        ORG org2 = orgservice.save(org_id, org);
        return ResponseEntity.status(HttpStatus.OK).body(org2);
    }

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

	@RequestMapping(method = RequestMethod.PUT, value = "/orgs/{org_id}")
    public ResponseEntity<ORG> update(@PathVariable("org_id") String org_id, @RequestBody ORG org) {
        ORG org2 = orgservice.update(org_id, org);
        return ResponseEntity.status(HttpStatus.OK).body(org2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/orgs/getdraft")
    public ResponseEntity<ORG> getDraft() {
        //ORG org = orgservice.getDraft( org_id);
        return ResponseEntity.status(HttpStatus.OK).body(new ORG());
    }

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


}
