package com.apobates.forum.thrones.admin.controller;

import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.apobates.forum.member.entity.MemberNamesProtect;
import com.apobates.forum.member.service.MemberNamesProtectService;
import com.apobates.forum.member.storage.core.MemberSessionBean;
import com.apobates.forum.thrones.admin.controller.form.MemberNamesProtectForm;
import com.apobates.forum.utils.TipMessage;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;

/**
 * 会员帐号保护控制器
 *
 * @author xiaofanku
 * @since 201900809
 */
@Controller
@RequestMapping(value = "/member/protect")
public class AdminMemberNamesProtectController {
    @Autowired
    private MemberNamesProtectService memberNamesProtectService;
    
    @GetMapping(path = "/")
    public String listPage(
            HttpServletRequest request,
            Model model) {
        model.addAttribute("rs", memberNamesProtectService.getAll().collect(Collectors.toList()));
        return "admin/member_protect/index";
    }
    
    @GetMapping(path = "/edit")
    public String editForm(
            @RequestParam(name = "id", required = false, defaultValue = "0") int id,
            HttpServletRequest request,
            Model model) {
        MemberNamesProtectForm form = new MemberNamesProtectForm();
        MemberNamesProtect mnp = memberNamesProtectService.get(id).orElse(MemberNamesProtect.empty());
        form.setRecord(mnp.getId());
        form.setStatus(mnp.isStatus());
        form.setNames(mnp.getMemberNames());
        model.addAttribute("form", form);
        //内置的提示
        model.addAttribute("nativeNames", String.join(",", MemberNamesProtect.getNativeProtect()));
        return "admin/member_protect/edit";
    }
    
    @PostMapping(path = "/edit")
    public String editAction(
            HttpServletRequest request,
            @Valid @ModelAttribute("form") MemberNamesProtectForm form,
            BindingResult bindingResult,
            Model model) {
        if (bindingResult.hasErrors()) {
            model.addAttribute("form", form);
            //内置的提示
            model.addAttribute("nativeNames", String.join(",", MemberNamesProtect.getNativeProtect()));
            return "admin/member_protect/edit";
        }
        if(MemberNamesProtect.getNativeProtect().contains(form.getNames())){
            model.addAttribute("errors", "内置的保护帐号不用创建");
            model.addAttribute("form", form);
            //内置的提示
            model.addAttribute("nativeNames", String.join(",", MemberNamesProtect.getNativeProtect()));
            return "admin/member_protect/edit";
        }
        MemberNamesProtect mnp = new MemberNamesProtect();
        mnp.setMemberNames(form.getNames());
        mnp.setStatus(form.getBooleanStatus());
        
        boolean symbol;
        if (form.isUpdate()) {
            symbol = memberNamesProtectService.edit(form.getIntegerRecord(), mnp).orElse(false);
        } else {
            symbol = memberNamesProtectService.create(mnp.getMemberNames(), mnp.isStatus()).isPresent();
        }
        if (symbol) {
            return "redirect:/member/protect/";
        }
        model.addAttribute("errors", form.getActionTitle() + "会员帐户保护操作失败");
        model.addAttribute("form", form);
        //内置的提示
        model.addAttribute("nativeNames", String.join(",", MemberNamesProtect.getNativeProtect()));
        return "admin/member_protect/edit";
    }
    
    //删除
    @PostMapping(path = "/remove", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public TipMessage removeAction(
            @RequestParam("id") int id,
            MemberSessionBean mbean,
            HttpServletRequest request,
            Model model) {
        return TipMessage.Builder.take(()->memberNamesProtectService.editStatus(id, false)).success("会员帐户保护删除成功").error("操作失败");
    }
}