package com.ecep.contract.service;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.vm.IdentityViewModel;

/**
 * 视图模型服务接口
 * 
 * @pahor 2025-08-02
 */
public interface ViewModelService<T extends IdentityEntity, TV extends IdentityViewModel<T>>
        extends IEntityService<T> {

    // <ID, R extends MyRepository<T, ID>> R getRepository();
    default String getBeanName() {
        String className = getClass().getSimpleName();
        // 按照Spring默认的bean命名规则转换
        // 1. 如果类名长度大于1且首两个字符都是大写，则不转换
        // 2. 否则，将首字母转为小写，其余部分保持不变
        if (className.length() > 1 && Character.isUpperCase(className.charAt(0))
                && Character.isUpperCase(className.charAt(1))) {
            return className;
        }
        // 将首字母转为小写
        if (className.length() > 0) {
            className = Character.toLowerCase(className.charAt(0)) + className.substring(1);
        }
        return className;
    }

    default T findById(Integer id) {
        return null;
    }

    default TV from(T entity) {
        TV model = createNewViewModel();
        model.update(entity);
        return model;
    }

    @SuppressWarnings("unchecked")
    default TV createNewViewModel() {
        try {
            Type[] types = getClass().getGenericInterfaces();
            // System.out.println("types = " + Arrays.asList(types));
            if (types.length > 0) {
                String typeName = types[0].getTypeName();
                // System.out.println("typeName = " + typeName);
                String clz = typeName.split("<")[1].split(">")[0].split(",")[1].trim();
                // System.out.println("clz = " + clz);
                Class<?> clazz = Class.forName(clz);
                return (TV) clazz.getDeclaredConstructor().newInstance();
            }
        } catch (Exception e) {
            throw new RuntimeException("无法创建ViewModel实例", e);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    default T createNewEntity() {
        try {
            Type[] types = getClass().getGenericInterfaces();
            // System.out.println("types = " + Arrays.asList(types));
            if (types.length > 0) {
                String typeName = types[0].getTypeName();
                // System.out.println("typeName = " + typeName);
                String clz = typeName.split("<")[1].split(">")[0].split(",")[0].trim();
                // System.out.println("clz = " + clz);
                Class<?> clazz = Class.forName(clz);
                return (T) clazz.getDeclaredConstructor().newInstance();
            }
        } catch (Exception e) {
            throw new RuntimeException("无法创建Entity实例", e);
        }
        return null;
    }

    @Override
    default T save(T entity) {
        throw new UnsupportedOperationException("Unimplemented method 'save'");
    }

    default void delete(T entity) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'delete'");
    }

    @Override
    default List<T> findAll() {
        return findAll(null, Pageable.unpaged()).getContent();
    }

    default Page<T> findAll(Map<String, Object> params, Pageable pageable) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'findAll'");
    }

    default long count(Map<String, Object> params) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'count'");
    }

    default Map<String, Object> getSpecification(String searchText) {
        HashMap<String, Object> params = new HashMap<>();
        params.put(ServiceConstant.KEY_SEARCH_TEXT, searchText);
        return params;
    }

}
