package com.eactive.eai.rms.ext.kakaocard.cm;

import java.nio.charset.StandardCharsets;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.spring.BaseController;

@Controller
@RequestMapping({"/deployresource"})
public class KakaoCardCmController extends BaseController {
   
   @Autowired
   private KakaoCardCmServiceFactory serviceFactory;

   @Autowired
   private KakaoCardCmDeploySuffixContextHolder kakaoCardCmDeploySuffixContextHolder;
   
   @GetMapping("/{serviceType}/{commitId}/deployStart.do")
   public ResponseEntity<Object> deployStart(@PathVariable String serviceType, @PathVariable String commitId) {
      
      serviceType = serviceType.toUpperCase();
      
      /** Invalid serviceType Error **/
      if (!DataSourceTypeManager.getDataSourceTypeNames().contains(serviceType))
         return ResponseEntity.internalServerError().contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
               .body("Invalid ServiceType: " + serviceType);
      
      try {
         
         DataSourceType dt = DataSourceTypeManager.getDataSourceType(serviceType);
         DataSourceContextHolder.setDataSourceType(dt);
         
         KakaoCardCmService service = serviceFactory.create(serviceType);
         
         service.startDeploy(serviceType, commitId);
         
      } catch (Exception e) {
         logger.error("KakaoCardCmController deployStart Error. serviceType=" + serviceType + ", commitId=" + commitId, e);
         return ResponseEntity.internalServerError().contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
               .body("KakaoCardCmController deployStart Error. serviceType=" + serviceType + ", commitId=" + commitId + ", error=" + e.getMessage());
      } finally {
         kakaoCardCmDeploySuffixContextHolder.remove();
      }
      
      return ResponseEntity.ok()
            .contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
            .body("Deploy Complete");
   }
   
   @GetMapping("/{serviceType}/{commitId}/promote.do")
   public ResponseEntity<Object> promote(@PathVariable String serviceType, @PathVariable String commitId) {
      
      serviceType = serviceType.toUpperCase();
      
      /** Invalid serviceType Error **/
      if (!DataSourceTypeManager.getDataSourceTypeNames().contains(serviceType))
         return ResponseEntity.internalServerError().contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
               .body("Invalid ServiceType: " + serviceType);
      
      try {
         
         DataSourceType dt = DataSourceTypeManager.getDataSourceType(serviceType);
         DataSourceContextHolder.setDataSourceType(dt);
         
         KakaoCardCmService service = serviceFactory.create(serviceType);
         
         service.promote(serviceType, commitId);
         
      } catch (Exception e) {
         logger.error("KakaoCardCmController promote Error. serviceType=" + serviceType + ", commitId=" + commitId, e);
         return ResponseEntity.internalServerError().contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
               .body("KakaoCardCmController promote Error. serviceType=" + serviceType + ", commitId=" + commitId + ", error=" + e.getMessage());
      } finally {
         
      }
      
      return ResponseEntity.ok()
            .contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
            .body("Promote Complete");
   }
   
   @GetMapping("/{serviceType}/{commitId}/rollback.do")
   public ResponseEntity<Object> rollback(@PathVariable String serviceType, @PathVariable String commitId) {
      
      serviceType = serviceType.toUpperCase();
      
      /** Invalid serviceType Error **/
      if (!DataSourceTypeManager.getDataSourceTypeNames().contains(serviceType))
         return ResponseEntity.internalServerError().contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
               .body("Invalid ServiceType: " + serviceType);
      
      try {
         
         DataSourceType dt = DataSourceTypeManager.getDataSourceType(serviceType);
         DataSourceContextHolder.setDataSourceType(dt);
         
         KakaoCardCmService service = serviceFactory.create(serviceType);
         
         service.rollback(serviceType, commitId);
         
      } catch (Exception e) {
         logger.error("KakaoCardCmController rollback Error. serviceType=" + serviceType + ", commitId=" + commitId, e);
         return ResponseEntity.internalServerError().contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
               .body("KakaoCardCmController rollback Error. serviceType=" + serviceType + ", commitId=" + commitId + ", error=" + e.getMessage());
      } finally {
         
      }
      
      return ResponseEntity.ok()
            .contentType(new MediaType("text", "plain", StandardCharsets.UTF_8))
            .body("Rollback Complete");
   }
   
}