package com.example.struts2;

import com.opensymphony.xwork2.ActionSupport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class OgnlLabAction extends ActionSupport {
    private String keyword;
    private Integer minAge;
    private DemoUser user = new DemoUser();
    private List<String> tags = new ArrayList<>();
    private List<DemoUser> users;
    private List<DemoUser> filteredUsers;

    public String execute() {
        loadUsers();
        filteredUsers = users;
        return SUCCESS;
    }

    public String bind() {
        loadUsers();
        filteredUsers = users.stream()
                .filter(u -> keyword == null || keyword.isBlank() || u.getName().contains(keyword) || u.getEmail().contains(keyword))
                .filter(u -> minAge == null || u.getAge() >= minAge)
                .collect(Collectors.toList());
        return SUCCESS;
    }

    private void loadUsers() {
        users = Arrays.asList(
                new DemoUser("张三", "zhangsan@example.com", 22),
                new DemoUser("李四", "lisi@example.com", 28),
                new DemoUser("王五", "wangwu@example.com", 34),
                new DemoUser("赵六", "zhaoliu@example.com", 19)
        );
    }

    public static class DemoUser {
        private String name;
        private String email;
        private Integer age;

        public DemoUser() {}
        public DemoUser(String name, String email, Integer age) {
            this.name = name;
            this.email = email;
            this.age = age;
        }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getEmail() { return email; }
        public void setEmail(String email) { this.email = email; }
        public Integer getAge() { return age; }
        public void setAge(Integer age) { this.age = age; }
    }

    public String getKeyword() { return keyword; }
    public void setKeyword(String keyword) { this.keyword = keyword; }
    public Integer getMinAge() { return minAge; }
    public void setMinAge(Integer minAge) { this.minAge = minAge; }
    public DemoUser getUser() { return user; }
    public void setUser(DemoUser user) { this.user = user; }
    public List<String> getTags() { return tags; }
    public void setTags(List<String> tags) { this.tags = tags; }
    public List<DemoUser> getUsers() { return users; }
    public List<DemoUser> getFilteredUsers() { return filteredUsers; }
}
