package com.dayou.aop;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@ControllerAdvice
public class CommonBindingInitializer {
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new MyStringEditor());
        binder.registerCustomEditor(Number.class,new MyNumberEditor());
        binder.registerCustomEditor(Date.class,new MyDateEditor());
        binder.registerCustomEditor(Boolean.class,new MyBooleanEditor());

        binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                if (StringUtils.isBlank(text)) {
                    setValue(null);
                }else {
                    if("null".equals(text) || "undefined".equals(text)){
                        setValue(null);
                    }else{
                        setValue(LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                    }

                }

            }
        });
        binder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                if (StringUtils.isBlank(text)) {
                    setValue(null);
                }else {
                    if("null".equals(text) || "undefined".equals(text)){
                        setValue(null);
                    }else{
                        setValue(LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    }

                }

            }
        });
        binder.registerCustomEditor(LocalTime.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                if (StringUtils.isBlank(text)) {
                    setValue(null);
                }else {
                    if("null".equals(text) || "undefined".equals(text)){
                        setValue(null);
                    }else{
                        setValue(LocalTime.parse(text, DateTimeFormatter.ofPattern("HH:mm:ss")));
                    }

                }

            }
        });
    }

    private class MyNumberEditor extends MyPropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            trimNull(text);
        }
    }

    private class MyBooleanEditor extends MyPropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            trimNull(text);
        }
    }

    private class MyDateEditor extends MyPropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            trimNull(text);
        }
    }

    private class MyPropertyEditorSupport extends PropertyEditorSupport {

        protected void trimNull(String text){
            if (StringUtils.isBlank(text)) {
                setValue(null);
            }else {
                if("null".equals(text) || "undefined".equals(text)){
                    setValue(null);
                }else{
                    setValue(text);
                }

            }
        }
    }

    private class MyStringEditor extends MyPropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            trimNull(text);
        }
    }



}
