package com.jfinal.club.ws;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName DocWsValidator
 * @Description TODO
 * @Author caolong
 * @Date 2020/3/24 18:21
 */
public class DocWsValidator extends Validator {


    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail( ));

        HttpServletRequest request = c.getRequest( );

        List< String > ygid = Arrays.asList(request.getParameterValues("yg.id"));
        List< String > ygmtype = Arrays.asList(request.getParameterValues("yg.mtype"));
        List< String > ygname = Arrays.asList(request.getParameterValues("yg.name"));
        List< String > ygbirthday = Arrays.asList(request.getParameterValues("yg.birthday"));
        List< String > yghujiAddress = Arrays.asList(request.getParameterValues("yg.hujiAddress"));
        List< String > ygphone = Arrays.asList(request.getParameterValues("yg.phone"));
        List< String > ygidType = Arrays.asList(request.getParameterValues("yg.idType"));
        List< String > ygidNumber = Arrays.asList(request.getParameterValues("yg.idNumber"));

        List< String > ygcompany = Arrays.asList(request.getParameterValues("yg.company"));
        List< String > yglegalPerson = Arrays.asList(request.getParameterValues("yg.legalPerson"));
        List< String > ygcompanyAddress = Arrays.asList(request.getParameterValues("yg.companyAddress"));
        List< String > ygcreditCode = Arrays.asList(request.getParameterValues("yg.creditCode"));
        List< String > yglegalPhone = Arrays.asList(request.getParameterValues("yg.legalPhone"));

        if (ygid != null && ygid.size( ) > 0) {
            for (int i = 0; i < ygid.size( ); i++) {
                if (ygmtype.get(i).equals("个人") || ygmtype.get(i).equals("0") || ygmtype.get(i) == null) {

                    if (ygmtype.get(i).equals("0")) {
                        addError("msg", "第" + (i + 1) + "原告人,请选择原告类型");
                    }
                    if (StringUtils.isBlank(ygname.get(i))) {
                        addError("msg", "第" + (i + 1) + "原告人,请填写姓名");
                    } else {
                        if (ygname.get(i).length( ) > 10) {
                            addError("msg", "第" + (i + 1) + "原告人,姓名长度不大于10个字符");
                        }
                    }

                    if (!StringUtils.isBlank(yghujiAddress.get(i)) && yghujiAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "原告人,户籍地址长度不大于250个字符");
                    }

                    String telreg = "^[0-9]{11}$";
                    if (!StringUtils.isBlank(ygphone.get(i)) && !ygphone.get(i).matches(telreg)) {
                        addError("msg", "第" + (i + 1) + "原告人,请输入11位手机号码!");
                    }

                    if (ygidType.get(i) != null) {
                        if (ygidType.get(i).equals("身份证")) {
                            String idreg = "^[0-9]{15}|[0-9]{17}[A-Za-z0-9_]{1}$";
                            if (!StringUtils.isBlank(ygidNumber.get(i)) && !ygidNumber.get(i).matches(idreg)) {
                                addError("msg", "第" + (i + 1) + "原告人,身份证输入有误,请修改后提交!");
                            }
                        }
                        if (ygidType.get(i).equals("护照")) {
                            String preg = "^[0-9]{9}$";
                            if (!StringUtils.isBlank(ygidNumber.get(i)) && !ygidNumber.get(i).matches(preg)) {
                                addError("msg", "第" + (i + 1) + "原告人,护照输入有误,请修改后提交!");
                            }
                        }
                    }

                } else {
                    if (StringUtils.isBlank(ygcompany.get(i))) {
                        addError("msg", "第" + (i + 1) + "位原告人,请输入公司名称");
                    } else {
                        if (ygcompany.get(i).length( ) > 30) {
                            addError("msg", "第" + (i + 1) + "位原告人,公司名称不能大于30个字符");
                        }
                    }

                    if (!StringUtils.isBlank(yglegalPerson.get(i)) && yglegalPerson.get(i).length( ) > 20) {
                        addError("msg", "第" + (i + 1) + "位原告人,法人姓名不能大于20个字符");
                    }


                    if (!StringUtils.isBlank(ygcompanyAddress.get(i)) && ygcompanyAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "位原告人,公司地址长度不大于250个字符.");
                    }

                    String ccreg = "^[A-Za-z0-9_]{18}$";
                    if (!StringUtils.isBlank(ygcreditCode.get(i)) && !ygcreditCode.get(i).matches(ccreg)) {
                        addError("msg", "第" + (i + 1) + "位原告人,信用代码输入有误,请修改后提交.");
                    }

                    String telreg = "^[0-9]{11}$";
                    if (!StringUtils.isBlank(yglegalPhone.get(i)) && !yglegalPhone.get(i).matches(telreg)) {
                        addError("msg", "第" + (i + 1) + "位原告人,请输入11位手机号码.");
                    }
                }

            }
        }

        List< String > bgid = Arrays.asList(request.getParameterValues("bg.id"));
        List< String > bgmtype = Arrays.asList(request.getParameterValues("bg.mtype"));
        List< String > bgname = Arrays.asList(request.getParameterValues("bg.name"));
        List< String > bgbirthday = Arrays.asList(request.getParameterValues("bg.birthday"));
        List< String > bghujiAddress = Arrays.asList(request.getParameterValues("bg.hujiAddress"));
        List< String > bgnowAddress = Arrays.asList(request.getParameterValues("bg.nowAddress"));
        List< String > bgphone = Arrays.asList(request.getParameterValues("bg.phone"));
        List< String > bgidType = Arrays.asList(request.getParameterValues("bg.idType"));
        List< String > bgidNumber = Arrays.asList(request.getParameterValues("bg.idNumber"));

        List< String > bgcompany = Arrays.asList(request.getParameterValues("bg.company"));
        List< String > bglegalPerson = Arrays.asList(request.getParameterValues("bg.legalPerson"));
        List< String > bgcompanyAddress = Arrays.asList(request.getParameterValues("bg.companyAddress"));
        List< String > bgcreditCode = Arrays.asList(request.getParameterValues("bg.creditCode"));
        List< String > bglegalPhone = Arrays.asList(request.getParameterValues("bg.legalPhone"));

        if (bgid != null && bgid.size( ) > 0) {
            for (int i = 0; i < bgid.size( ); i++) {
                if (bgmtype.get(i).equals("个人") || bgmtype.get(i).equals("0") || bgmtype.get(i) == null) {
                    if (bgmtype.get(i).equals("0")) {
                        addError("msg", "第" + (i + 1) + "被告人,请选择原告类型");
                    }
                    if (StringUtils.isBlank(bgname.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,请填写姓名");
                    } else {
                        if (bgname.get(i).length( ) > 10) {
                            addError("msg", "第" + (i + 1) + "被告人,姓名长度不大于10个字符");
                        }
                    }

                    if (!StringUtils.isBlank(bghujiAddress.get(i)) && bghujiAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "被告人,户籍地址长度不大于250个字符");
                    }


                    if (!StringUtils.isBlank(bgnowAddress.get(i)) && bgnowAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "被告人,现住址长度不大于250个字符");
                    }

                    String telreg = "^[0-9]{11}$";
                    if (!StringUtils.isBlank(bgphone.get(i)) && !bgphone.get(i).matches(telreg)) {
                        addError("msg", "第" + (i + 1) + "被告人,请输入11位手机号码!");
                    }

                    if (bgidType.get(i) != null) {
                        if (bgidType.get(i).equals("身份证")) {
                            String idreg = "^[0-9]{15}|[0-9]{17}[A-Za-z0-9_]{1}$";
                            if (!StringUtils.isBlank(bgidNumber.get(i)) && !bgidNumber.get(i).matches(idreg)) {
                                addError("msg", "第" + (i + 1) + "被告人,身份证输入有误,请修改后提交!");
                            }
                        }
                        if (bgidType.get(i).equals("护照")) {
                            String preg = "^[0-9]{9}$";
                            if (!StringUtils.isBlank(bgidNumber.get(i)) && !bgidNumber.get(i).matches(preg)) {
                                addError("msg", "第" + (i + 1) + "被告人,护照输入有误,请修改后提交!");
                            }
                        }
                    }
                } else {
                    if (StringUtils.isBlank(bgcompany.get(i))) {
                        addError("msg", "第" + (i + 1) + "位被告人,请输入公司名称");
                    } else {
                        if (bgcompany.get(i).length( ) > 30) {
                            addError("msg", "第" + (i + 1) + "位被告人,公司名称不能大于30个字符");
                        }
                    }

                    if (!StringUtils.isBlank(bglegalPerson.get(i)) && bglegalPerson.get(i).length( ) > 20) {
                        addError("msg", "第" + (i + 1) + "位被告人,法人姓名不能大于20个字符");
                    }


                    if (!StringUtils.isBlank(bgcompanyAddress.get(i)) && bgcompanyAddress.get(i).length( ) > 250) {
                        addError("msg", "第" + (i + 1) + "位被告人,公司地址长度不大于250个字符");
                    }


                    String ccreg = "^[A-Za-z0-9_]{18}$";
                    if (!StringUtils.isBlank(bgcreditCode.get(i)) && !bgcreditCode.get(i).matches(ccreg)) {
                        addError("msg", "第" + (i + 1) + "位被告人,信用代码输入有误,请修改后提交");
                    }

                    String telreg = "^[0-9]{11}$";
                    if (!StringUtils.isBlank(bglegalPhone.get(i)) && !bglegalPhone.get(i).matches(telreg)) {
                        addError("msg", "第" + (i + 1) + "位被告人,请输入11位手机号码!");
                    }
                }

            }
        }

        validateRequiredString("wsinfo.wsRequest", "msg", "请填写诉讼请求!");
        validateRequiredString("wsinfo.wsReason", "msg", "请填写事实理由!");

        if (c.get("wsinfo.wsRequest").length( ) > 5000) {
            addError("msg", "诉讼请求不能大于5000个字符!");
        }

        if (c.get("wsinfo.wsReason").length( ) > 5000) {
            addError("msg", "事实理由不能大于5000个字符!");
        }


        String moneyReg = "^(([1-9][0-9]{0,6})|([0]{1})|(([0]\\.\\d{1,2}|[1-9][0-9]{0,14}\\.\\d{1,2})))$";
        if (c.get("wsinfo.money") != null && !c.get("wsinfo.money").matches(moneyReg)) {
            addError("msg", "金额输入有误!");
        }

        if (c.get("wsinfo.materialName") != null && c.get("wsinfo.materialName").length( ) > 50) {
            addError("msg", "材料名称不能大于50个字符!");
        }

        if (c.get("wsinfo.evidence") != null && c.get("wsinfo.evidence").length( ) > 5000) {
            addError("msg", "证据内容不能大于5000字符!");
        }
    }


    @Override
    protected void handleError(Controller c) {
        Ret ret = getRet( );
        c.renderJson(ret);
    }


/*
    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail( ));

        HttpServletRequest request = c.getRequest( );

        List< String > ygid = Arrays.asList(request.getParameterValues("yg.id"));
        List< String > ygmtype = Arrays.asList(request.getParameterValues("yg.mtype"));
        List< String > ygname = Arrays.asList(request.getParameterValues("yg.name"));
        List< String > ygbirthday = Arrays.asList(request.getParameterValues("yg.birthday"));
        List< String > yghujiAddress = Arrays.asList(request.getParameterValues("yg.hujiAddress"));
        List< String > ygphone = Arrays.asList(request.getParameterValues("yg.phone"));
        List< String > ygidType = Arrays.asList(request.getParameterValues("yg.idType"));
        List< String > ygidNumber = Arrays.asList(request.getParameterValues("yg.idNumber"));

        List< String > ygcompany = Arrays.asList(request.getParameterValues("yg.company"));
        List< String > yglegalPerson = Arrays.asList(request.getParameterValues("yg.legalPerson"));
        List< String > ygcompanyAddress = Arrays.asList(request.getParameterValues("yg.companyAddress"));
        List< String > ygcreditCode = Arrays.asList(request.getParameterValues("yg.creditCode"));
        List< String > yglegalPhone = Arrays.asList(request.getParameterValues("yg.legalPhone"));

        if (ygid != null && ygid.size( ) > 0) {
            for (int i = 0; i < ygid.size( ); i++) {
                if (ygmtype.get(i).equals("个人") || ygmtype.get(i).equals("0") || ygmtype.get(i) == null) {

                    if (ygmtype.get(i).equals("0")) {
                        addError("msg", "第" + (i + 1) + "原告人,请选择原告类型");
                    }
                    if (StringUtils.isBlank(ygname.get(i))) {
                        addError("msg", "第" + (i + 1) + "原告人,请填写姓名");
                    } else {
                        if (ygname.get(i).length( ) > 10) {
                            addError("msg", "第" + (i + 1) + "原告人,姓名长度不大于10个字符");
                        }
                    }

                    if (StringUtils.isBlank(ygbirthday.get(i))) {
                        addError("msg", "第" + (i + 1) + "原告人,请选择出生日期");
                    }

                    if (StringUtils.isBlank(yghujiAddress.get(i))) {
                        addError("msg", "第" + (i + 1) + "原告人,请填写户籍地址");
                    } else {
                        if (yghujiAddress.get(i).length( ) > 250) {
                            addError("msg", "第" + (i + 1) + "原告人,户籍地址长度不大于250个字符");
                        }
                    }

                    if (StringUtils.isBlank(ygphone.get(i))) {
                        addError("msg", "第" + (i + 1) + "原告人,手机号码不能为空");
                    } else {
                        String telreg = "^[0-9]{11}$";
                        if (!ygphone.get(i).matches(telreg)) {
                            addError("msg", "第" + (i + 1) + "原告人,请输入11位手机号码!");
                        }
                    }

                    if (ygidType.get(i) != null) {
                        if (ygidType.get(i).equals("身份证")) {
                            if (StringUtils.isBlank(ygidNumber.get(i))) {
                                addError("msg", "第" + (i + 1) + "原告人,身份证号不能为空");
                            }
                            String idreg = "^[0-9]{15}|[0-9]{17}[A-Za-z0-9_]{1}$";
                            if (!ygidNumber.get(i).matches(idreg)) {
                                addError("msg", "第" + (i + 1) + "原告人,身份证输入有误,请修改后提交!");
                            }
                        }
                        if (ygidType.get(i).equals("护照")) {
                            if (StringUtils.isBlank(ygidNumber.get(i))) {
                                addError("msg", "第" + (i + 1) + "原告人,护照号不能为空");
                            }
                            String preg = "^[0-9]{9}$";
                            if (!ygidNumber.get(i).matches(preg)) {
                                addError("msg", "第" + (i + 1) + "原告人,护照输入有误,请修改后提交!");
                            }
                        }
                    }

                }else{
                    if(StringUtils.isBlank(ygcompany.get(i))){
                        addError("msg","第" + (i + 1) + "位原告人,请输入公司名称");
                    }else{
                        if(ygcompany.get(i).length() > 30){
                            addError("msg","第" + (i + 1) + "位原告人,公司名称不能大于30个字符");
                        }
                    }

                    if(StringUtils.isBlank(yglegalPerson.get(i))){
                        addError("msg","第" + (i + 1) + "位原告人,请输入法人名称");
                    }else{
                        if(yglegalPerson.get(i).length() > 20){
                            addError("msg","第" + (i + 1) + "位原告人,法人姓名不能大于20个字符");
                        }
                    }


                    if (StringUtils.isBlank(ygcompanyAddress.get(i))) {
                        addError("msg", "第" + (i + 1) + "位原告人,请填写公司地址");
                    } else {
                        if (ygcompanyAddress.get(i).length( ) > 250) {
                            addError("msg", "第" + (i + 1) + "位原告人,公司地址长度不大于250个字符");
                        }
                    }


                    if (StringUtils.isBlank(ygcreditCode.get(i))) {
                        addError("msg", "第" + (i + 1) + "位原告人,请填写信用代码");
                    } else {
                        String ccreg = "^[A-Za-z0-9_]{18}$";
                        if (!ygcreditCode.get(i).matches(ccreg )) {
                            addError("msg", "第" + (i + 1) + "位原告人,信用代码输入有误,请修改后提交");
                        }
                    }

                    if (StringUtils.isBlank(yglegalPhone.get(i))) {
                        addError("msg", "第" + (i + 1) + "位原告人,手机号码不能为空");
                    } else {
                        String telreg = "^[0-9]{11}$";
                        if (!yglegalPhone .get(i).matches(telreg)) {
                            addError("msg", "第" + (i + 1) + "位原告人,请输入11位手机号码!");
                        }
                    }
                }

            }
        }

        List< String > bgid = Arrays.asList(request.getParameterValues("bg.id"));
        List< String > bgmtype = Arrays.asList(request.getParameterValues("bg.mtype"));
        List< String > bgname = Arrays.asList(request.getParameterValues("bg.name"));
        List< String > bgbirthday = Arrays.asList(request.getParameterValues("bg.birthday"));
        List< String > bghujiAddress = Arrays.asList(request.getParameterValues("bg.hujiAddress"));
        List< String > bgnowAddress = Arrays.asList(request.getParameterValues("bg.nowAddress"));
        List< String > bgphone = Arrays.asList(request.getParameterValues("bg.phone"));
        List< String > bgidType = Arrays.asList(request.getParameterValues("bg.idType"));
        List< String > bgidNumber = Arrays.asList(request.getParameterValues("bg.idNumber"));

        List< String > bgcompany = Arrays.asList(request.getParameterValues("bg.company"));
        List< String > bglegalPerson = Arrays.asList(request.getParameterValues("bg.legalPerson"));
        List< String > bgcompanyAddress = Arrays.asList(request.getParameterValues("bg.companyAddress"));
        List< String > bgcreditCode = Arrays.asList(request.getParameterValues("bg.creditCode"));
        List< String > bglegalPhone = Arrays.asList(request.getParameterValues("bg.legalPhone"));

        if (bgid != null && bgid.size( ) > 0) {
            for (int i = 0; i < bgid.size( ); i++) {
                if (bgmtype.get(i).equals("个人") || bgmtype.get(i).equals("0") || bgmtype.get(i) == null) {
                    if (bgmtype.get(i).equals("0")) {
                        addError("msg", "第" + (i + 1) + "被告人,请选择原告类型");
                    }
                    if (StringUtils.isBlank(bgname.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,请填写姓名");
                    } else {
                        if (bgname.get(i).length( ) > 10) {
                            addError("msg", "第" + (i + 1) + "被告人,姓名长度不大于10个字符");
                        }
                    }

                    if (StringUtils.isBlank(bgbirthday.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,请选择出生日期");
                    }

                    if (StringUtils.isBlank(bghujiAddress.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,请填写户籍地址");
                    } else {
                        if (bghujiAddress.get(i).length( ) > 250) {
                            addError("msg", "第" + (i + 1) + "被告人,户籍地址长度不大于250个字符");
                        }
                    }


                    if (StringUtils.isBlank(bgnowAddress.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,请填写现在住址");
                    } else {
                        if (bgnowAddress.get(i).length( ) > 250) {
                            addError("msg", "第" + (i + 1) + "被告人,现住址长度不大于250个字符");
                        }
                    }

                    if (StringUtils.isBlank(bgphone.get(i))) {
                        addError("msg", "第" + (i + 1) + "被告人,手机号码不能为空");
                    } else {
                        String telreg = "^[0-9]{11}$";
                        if (!bgphone.get(i).matches(telreg)) {
                            addError("msg", "第" + (i + 1) + "被告人,请输入11位手机号码!");
                        }
                    }

                    if (bgidType.get(i) != null) {
                        if (bgidType.get(i).equals("身份证")) {
                            if (StringUtils.isBlank(bgidNumber.get(i))) {
                                addError("msg", "第" + (i + 1) + "被告人,身份证号不能为空");
                            }
                            String idreg = "^[0-9]{15}|[0-9]{17}[A-Za-z0-9_]{1}$";
                            if (!bgidNumber.get(i).matches(idreg)) {
                                addError("msg", "第" + (i + 1) + "被告人,身份证输入有误,请修改后提交!");
                            }
                        }
                        if (bgidType.get(i).equals("护照")) {
                            if (StringUtils.isBlank(bgidNumber.get(i))) {
                                addError("msg", "第" + (i + 1) + "被告人,护照号不能为空");
                            }
                            String preg = "^[0-9]{9}$";
                            if (!bgidNumber.get(i).matches(preg)) {
                                addError("msg", "第" + (i + 1) + "被告人,护照输入有误,请修改后提交!");
                            }
                        }
                    }
                }else{
                    if(StringUtils.isBlank(bgcompany.get(i))){
                        addError("msg","第" + (i + 1) + "位被告人,请输入公司名称");
                    }else{
                        if(bgcompany.get(i).length() > 30){
                            addError("msg","第" + (i + 1) + "位被告人,公司名称不能大于30个字符");
                        }
                    }

                    if(StringUtils.isBlank(bglegalPerson.get(i))){
                        addError("msg","第" + (i + 1) + "位被告人,请输入法人名称");
                    }else{
                        if(bglegalPerson.get(i).length() > 20){
                            addError("msg","第" + (i + 1) + "位被告人,法人姓名不能大于20个字符");
                        }
                    }


                    if (StringUtils.isBlank(bgcompanyAddress.get(i))) {
                        addError("msg", "第" + (i + 1) + "位被告人,请填写公司地址");
                    } else {
                        if (bgcompanyAddress.get(i).length( ) > 250) {
                            addError("msg", "第" + (i + 1) + "位被告人,公司地址长度不大于250个字符");
                        }
                    }


                    if (StringUtils.isBlank(bgcreditCode.get(i))) {
                        addError("msg", "第" + (i + 1) + "位被告人,请填写信用代码");
                    } else {
                        String ccreg = "^[A-Za-z0-9_]{18}$";
                        if (!bgcreditCode.get(i).matches(ccreg )) {
                            addError("msg", "第" + (i + 1) + "位被告人,信用代码输入有误,请修改后提交");
                        }
                    }

                    if (StringUtils.isBlank(bglegalPhone.get(i))) {
                        addError("msg", "第" + (i + 1) + "位被告人,手机号码不能为空");
                    } else {
                        String telreg = "^[0-9]{11}$";
                        if (!bglegalPhone .get(i).matches(telreg)) {
                            addError("msg", "第" + (i + 1) + "位被告人,请输入11位手机号码!");
                        }
                    }
                }

            }
        }

        validateRequiredString("wsinfo.wsRequest", "msg", "请填写诉讼请求!");
        validateRequiredString("wsinfo.wsReason", "msg", "请填写事实理由!");

        if (c.get("wsinfo.wsRequest").length( ) > 5000) {
            addError("msg", "诉讼请求不能大于5000个字符!");
        }

        if (c.get("wsinfo.wsReason").length( ) > 5000) {
            addError("msg", "事实理由不能大于5000个字符!");
        }


        String moneyReg = "^(([1-9][0-9]{0,6})|([0]{1})|(([0]\\.\\d{1,2}|[1-9][0-9]{0,14}\\.\\d{1,2})))$";
        if (c.get("wsinfo.money") != null && !c.get("wsinfo.money").matches(moneyReg)) {
            addError("msg", "金额输入有误!");
        }

        if (c.get("wsinfo.materialName") != null && c.get("wsinfo.materialName").length( ) > 50) {
            addError("msg", "材料名称不能大于50个字符!");
        }

        if (c.get("wsinfo.evidence") != null && c.get("wsinfo.evidence").length( ) > 5000) {
            addError("msg", "证据内容不能大于5000字符!");
        }
    }
*/

}
