package com.chantha.springdemo.controller;

import com.chantha.springdemo.model.Product;
import com.chantha.springdemo.service.ProductService;
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 org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/product")
public class ProductRestController {

    private ProductService productService;
    private Map<String,Object> map=null;
    private HttpStatus status=null;

    @Autowired
    public ProductRestController(ProductService productService) {
        this.productService = productService;
    }

    @GetMapping(value = "/findAll",headers = "Accept=application/json")
    public ResponseEntity<Map<String,Object>> findAllProduct(){
        List<Product> product=null;
        map=new HashMap<>();
        try{
            product=productService.findAllProduct();
            if(!product.isEmpty()){
                map.put("data",product);
                map.put("message","Get product successfully");
                map.put("status",true);
                status=HttpStatus.ACCEPTED;
            }
            else{
                map.put("message","Cannot get product");
                map.put("status",false);
                status=HttpStatus.NOT_FOUND;
            }
        }
        catch (Exception ex){
            map.put("message","Something is broken");
            map.put("status",false);
            status=HttpStatus.INTERNAL_SERVER_ERROR;
            ex.printStackTrace();
        }
        return new ResponseEntity<>(map, status);
    }

    @PostMapping(value = "/save",headers = "Accept=application/json")
    public ResponseEntity<Map<String,Object>> save(@RequestBody Product product, HttpServletRequest request){
        map=new HashMap<>();
        try{
            productService.save(product);
           Product pro=productService.findById(product.getId());
            if(pro != null){
                map.put("data",product);
                map.put("message","insert product successfully");
                map.put("status",true);
                status=HttpStatus.CREATED;
            }
            else{
                map.put("message","Cannot insert product");
                map.put("status",false);
                status=HttpStatus.NOT_FOUND;
            }
        }
        catch (Exception ex){
            map.put("message","Something is broken");
            map.put("status",false);
            status=HttpStatus.INTERNAL_SERVER_ERROR;
            ex.printStackTrace();
        }
        return new ResponseEntity<Map<String,Object>>(map,status);
    }
    @PutMapping(value = "/update",headers = "Accept=application/json")
    public ResponseEntity<Map<String,Object>> update(@RequestBody Product product, HttpServletRequest request){
        map=new HashMap<>();
        try{
            Product pro=productService.findById(product.getId());
            if(pro != null){
                productService.save(product);
                map.put("data",product);
                map.put("message","update product successfully");
                map.put("status",true);
                status=HttpStatus.ACCEPTED;
            }
            else{
                map.put("message","Cannot update product");
                map.put("status",false);
                status=HttpStatus.NOT_FOUND;
            }
        }
        catch (Exception ex){
            map.put("message","Something is broken");
            map.put("status",false);
            status=HttpStatus.INTERNAL_SERVER_ERROR;
            ex.printStackTrace();
        }
        return new ResponseEntity<Map<String,Object>>(map,status);
    }
    @PatchMapping(value = "/delete/{id}",headers = "Accept=application/json")
    public ResponseEntity<Map<String,Object>> delete(@PathVariable("id") Long id){
        map=new HashMap<>();
        try{
            Product pro=productService.findById(id);
            if(pro != null){
                productService.deleteById(id);
                map.put("message","delete product successfully");
                map.put("status",true);
                status=HttpStatus.ACCEPTED;
            }
            else{
                map.put("message","Can not delete product");
                map.put("status",false);
                status=HttpStatus.NOT_FOUND;
            }
        }
        catch (Exception ex){
            map.put("message","Something is broken");
            map.put("status",false);
            status=HttpStatus.INTERNAL_SERVER_ERROR;
            ex.printStackTrace();
        }
        return new ResponseEntity<Map<String,Object>>(map,status);
    }

}
