package cn.ibizlab.core.data.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.math.BigInteger;

import cn.ibizlab.core.data.domain.DOModel;
import cn.ibizlab.core.data.lite.EntityModel;
import cn.ibizlab.core.data.lite.LiteStorage;
import cn.ibizlab.core.data.model.PojoSchema;
import cn.ibizlab.core.data.model.TransUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import cn.ibizlab.util.errors.BadRequestAlertException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.core.data.domain.DTOModel;
import cn.ibizlab.core.data.filter.DTOModelSearchContext;
import cn.ibizlab.core.data.service.IDTOModelService;

import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.helper.DEFieldCacheMap;


/**
 * 实体[数据传输对象] 无存储服务对象接口实现
 */
@Slf4j
@Service
public class DTOModelServiceImpl implements IDTOModelService {

    @Value("${ibiz.model.path:/app/file/model/}")
    private String modelPath;

    @Override
    public boolean create(DTOModel et) {
        //代码实现
        return true;
    }

    public void createBatch(List<DTOModel> list){
    }

    @Override
    public boolean update(DTOModel et) {
        //代码实现
        return true;
    }

    public void updateBatch(List<DTOModel> list){
    }

    @Override
    public boolean remove(String key) {
        return true;
    }

    public void removeBatch(Collection<String> idList){
    }

    @Override
    public DTOModel get(String key) {
        DTOModel dtoModel=new DTOModel();
        PojoSchema schema= null;
        String[] args=key.split("[.]");
        String system=args[0];
        String dto=args[2];
        File domainDir = new File(modelPath+system+File.separator+"dto");
        if(!domainDir.exists())
            domainDir.mkdirs();

        File schemaJson=new File(domainDir,dto+".json");
        if(!schemaJson.exists())
        {
            String entity=dto;
            if(entity.endsWith("DTO"))
                entity=entity.substring(0,entity.length()-3);
            EntityModel entityModel= LiteStorage.getLiteModelService().getProxyEntityModel(system,entity);
            if(entityModel!=null) {
                schema= JSONObject.parseObject(JSON.toJSONString(TransUtils.LiteEntityModelModel2Schema(entityModel)),PojoSchema.class);
                schema.setName(dto);
                schema.setId(key);
            }
        }
        else
        {
            try {
                String schemaJSON=new String(Files.readAllBytes(Paths.get(domainDir.getAbsolutePath(),dto+".json")), StandardCharsets.UTF_8);
                schema= JSON.parseObject(schemaJSON,PojoSchema.class);
            } catch (IOException e) {

            }
        }

        if(schema!=null)
        {
            dtoModel.setSchema(schema);
            try {
                Files.write(Paths.get(domainDir.getAbsolutePath(),dto+".json"), JSON.toJSONString(schema).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
            } catch (IOException e) {

            }
        }
        else
        {
            throw new BadRequestAlertException("未找到对应的模型","DOModel",key);
        }


        return dtoModel;
    }

    @Override
    public DTOModel getDraft(DTOModel et) {
        return et;
    }

    @Override
    public boolean checkKey(DTOModel et) {
        return false;
    }
    @Override
    @Transactional
    public boolean save(DTOModel et) {
        //代码实现
        return true;
    }

    @Override
    public void saveBatch(List<DTOModel> list) {
    }





    /**
     * 查询集合 数据集
     */
    @Override
    public Page<DTOModel> searchDefault(DTOModelSearchContext context) {
        return new PageImpl<DTOModel>(new ArrayList(),context.getPageable(),0);
    }

}


