package com.lightinit.hsdataplatformresdir.controller.admin;


import com.lightinit.hsdataplatformresdir.dictionary.DicAuthorizationState;
import com.lightinit.hsdataplatformresdir.dictionary.DicPlatformType;
import com.lightinit.hsdataplatformresdir.dictionary.DicResourceType;
import com.lightinit.hsdataplatformresdir.entity.BaseExample;
import com.lightinit.hsdataplatformresdir.model.ResultState;
import com.lightinit.hsdataplatformresdir.model.admin.AuthorizationHeableModel;
import com.lightinit.hsdataplatformresdir.model.admin.AuthorizationListModel;
import com.lightinit.hsdataplatformresdir.model.admin.AuthorizationSearchModel;
import com.lightinit.hsdataplatformresdir.model.admin.LoginUser;
import com.lightinit.hsdataplatformresdir.service.admin.IAuthorizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin/authorization")
public class AuthorizationController extends BaseController{

    @Autowired
    private IAuthorizationService authorizationService ;

    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/authorization/index");
        modelAndView.addObject("authorizationState", DicAuthorizationState.getInstacne().getDicMap()) ;
        modelAndView.addObject("resourceType", DicResourceType.getInstacne().getDicMap()) ;
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(AuthorizationSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo,HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ModelAndView modelAndView = new ModelAndView("admin/authorization/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        int isFlag = authorizationService.isAdminFalg(loginUser.getId()) ;
        if (!(isFlag>0)){
            inputModel.setOrgId(loginUser.getOrgId());
        }
        List<AuthorizationListModel> outputModel = authorizationService.queryList(inputModel);
        pager.setTotal(authorizationService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        modelAndView.addObject("open", DicPlatformType.OPEN);
        modelAndView.addObject("share", DicPlatformType.SHARE);
        modelAndView.addObject("state", DicAuthorizationState.AUTHORIZE_WAIT);
        return modelAndView;
    }

    @RequestMapping(value = "handle.htm/{id}")
    public ModelAndView handle(@PathVariable long id,String platfromType,String outId1,long applier){
        ModelAndView modelAndView = new ModelAndView("admin/authorization/handle");
        modelAndView.addObject("authorization", authorizationService.queryOneView(id));
        modelAndView.addObject("open", DicPlatformType.OPEN);
        modelAndView.addObject("share", DicPlatformType.SHARE);
        modelAndView.addObject("outId1", outId1);
        modelAndView.addObject("applier", applier);
        modelAndView.addObject("platfromType",platfromType);
        return modelAndView;
    }

    @RequestMapping(value = "handle.action")
    @ResponseBody
    public ResultState doEdit( @Valid AuthorizationHeableModel inputModel, BindingResult bindingResult,HttpSession session){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        ResultState<Long> resultState = authorizationService.handle(inputModel,loginUser.getId());
        return resultState;
    }

    @RequestMapping(value = "view.htm/{id}")
    public ModelAndView view(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/authorization/view");
        modelAndView.addObject("authorization", authorizationService.queryOneView(id));
        modelAndView.addObject("open", DicPlatformType.OPEN);
        modelAndView.addObject("share", DicPlatformType.SHARE);
        return modelAndView;
    }
}
