package org.springblade.flow.visual.onlinedev.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.base.util.FormCloumnUtil;
import org.springblade.flow.database.exception.DataException;
import org.springblade.flow.model.FormAllModel;
import org.springblade.flow.model.FormColumnModel;
import org.springblade.flow.model.FormEnum;
import org.springblade.flow.model.visiual.ColumnDataModel;
import org.springblade.flow.model.visiual.FormDataModel;
import org.springblade.flow.model.visiual.TableModel;
import org.springblade.flow.model.visiual.fields.FieldsModel;
import org.springblade.flow.model.visiual.fields.config.ConfigModel;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.JsonUtilEx;
import org.springblade.flow.util.StringUtil;
import org.springblade.flow.visual.base.entity.VisualdevEntity;
import org.springblade.flow.visual.base.service.VisualdevService;
import org.springblade.flow.visual.onlinedev.entity.VisualdevModelDataEntity;
import org.springblade.flow.visual.onlinedev.model.PaginationModel;
import org.springblade.flow.visual.onlinedev.model.VisualdevModelDataCrForm;
import org.springblade.flow.visual.onlinedev.model.VisualdevModelDataInfoVO;
import org.springblade.flow.visual.onlinedev.model.VisualdevModelDataUpForm;
import org.springblade.flow.visual.onlinedev.service.VisualdevModelAppService;
import org.springblade.flow.visual.onlinedev.service.VisualdevModelDataService;
import org.springblade.flow.visual.onlinedev.util.AutoFeildsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @author JNPF开发平台组
 * @date 2021/3/16
 */
@Service
public class VisualdevModelAppServiceImpl implements VisualdevModelAppService {

    @Autowired
    private VisualdevService visualdevService;
    @Autowired
    private VisualdevModelDataService visualdevModelDataService;

    @Override
    public List<Map<String, Object>> resultList(String modelId, PaginationModel paginationModel) throws DataException, ParseException, SQLException, IOException {
        VisualdevEntity entity = visualdevService.getInfo(modelId);
        //赋值type
        ColumnDataModel columnDataModel = JsonUtil.getJsonToBean(entity.getColumnData(), ColumnDataModel.class);
        columnDataModel.setType(1);
        entity.setColumnData(JsonUtilEx.getObjectToString(columnDataModel));
        common(entity);
        List<Map<String, Object>> realList = visualdevModelDataService.getListResult(entity, paginationModel);
        //排序
        if (StringUtil.isNotEmpty(paginationModel.getSidx()) && realList.size() > 0) {
            Object value = realList.get(0).get(paginationModel.getSidx());
            if (value != null) {
                if ("desc".equals(paginationModel.getSort())) {
                    realList.sort(Comparator.comparing((Map<String, Object> h) -> ((String) h.get(paginationModel.getSidx()))).reversed());
                } else {
                    realList.sort(Comparator.comparing((Map<String, Object> h) -> ((String) h.get(paginationModel.getSidx()))));
                }
            }
        }
        return realList;
    }

    @Override
    public void create(VisualdevEntity entity, String data) throws DataException, SQLException {
        VisualdevModelDataCrForm form = new VisualdevModelDataCrForm();
        form.setData(data);
        common(entity);
        visualdevModelDataService.create(entity, form);
    }

    @Override
    public ActionResult update(String id, VisualdevEntity entity, String data) throws DataException, SQLException {
        VisualdevModelDataUpForm form = new VisualdevModelDataUpForm();
        form.setData(data);
        common(entity);
        return visualdevModelDataService.update(id, entity, form);
    }

    @Override
    public boolean delete(String id, VisualdevEntity entity) throws DataException, SQLException {
        boolean flag = false;
        List<TableModel> tableModelList = JsonUtil.getJsonToList(entity.getTables(), TableModel.class);
        if (tableModelList.size() > 0) {
            flag = visualdevModelDataService.tableDelete(id, entity);
        } else {
            VisualdevModelDataEntity dataEntity = visualdevModelDataService.getInfo(id);
            if (dataEntity != null) {
                visualdevModelDataService.delete(dataEntity);
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public Map<String, Object> info(String id, VisualdevEntity entity) throws DataException, ParseException, SQLException, IOException {
        List<TableModel> tableModelList = JsonUtil.getJsonToList(entity.getTables(), TableModel.class);
        VisualdevModelDataInfoVO vo = new VisualdevModelDataInfoVO();
        if (tableModelList.size() > 0) {
            common(entity);
            vo = visualdevModelDataService.tableInfo(id, entity);
        } else {
            VisualdevModelDataEntity dataEntity = visualdevModelDataService.getInfo(id);
            List<FieldsModel> list = info(entity);
            if (dataEntity != null) {
                String data = AutoFeildsUtil.autoFeilds(list, dataEntity.getData());
                vo.setData(data);
                vo.setId(id);
            }
        }
        Map<String, Object> result = JsonUtil.entityToMap(vo);
        return result;
    }

    /**
     * 信息去多余控件
     * @param entity
     * @return
     */
    private List<FieldsModel> info(VisualdevEntity entity) {
        //修改app属性没有默认值
        FormDataModel formDataModel = JsonUtil.getJsonToBean(entity.getFormData(), FormDataModel.class);
        List<FieldsModel> formModel = JsonUtil.getJsonToList(formDataModel.getFields(), FieldsModel.class);
        List<FieldsModel> fieldsAll = fieldsAll(formModel);
        return fieldsAll;
    }

    /**
     * 查询、新增和修改属性
     * @param entity
     */
    private void common(VisualdevEntity entity) {
        //修改app属性没有默认值
        FormDataModel formDataModel = JsonUtil.getJsonToBean(entity.getFormData(), FormDataModel.class);
        List<FieldsModel> formModel = JsonUtil.getJsonToList(formDataModel.getFields(), FieldsModel.class);
        List<FieldsModel> fieldsAll = fieldsAll(formModel);
        Map<String, Object> map = new HashMap<>(16);
        map.put("fields", fieldsAll);
        entity.setFormData(JsonUtil.getObjectToString(map));
    }

    /**
     * app的默认值
     * @param formModel
     * @return
     */
    private List<FieldsModel> fieldsAll(List<FieldsModel> formModel) {
        //修改app属性没有默认值
        List<FormAllModel> formAllModel = new ArrayList<>();
        FormCloumnUtil.recursionForm(formModel, formAllModel);
        //赋值主表的日期类型
        List<FormAllModel> formAll = formAllModel.stream().filter(t -> FormEnum.mast.getMessage().equals(t.getJnpfKey()) || FormEnum.table.getMessage().equals(t.getJnpfKey())).collect(Collectors.toList());
        List<FieldsModel> fieldsAll = new ArrayList<>();
        for (FormAllModel model : formAll) {
            if (FormEnum.mast.getMessage().equals(model.getJnpfKey())) {
                FieldsModel fieLdsModel = model.getFormColumnModel().getFieLdsModel();
                model(fieLdsModel);
                fieldsAll.add(fieLdsModel);
            } else {
                String tabVmodel = model.getChildList().getTableModel();
                FieldsModel child = new FieldsModel();
                child.setVModel(tabVmodel);
                String tableName = model.getChildList().getTableName();
                ConfigModel configModel = new ConfigModel();
                configModel.setTableName(tableName);
                configModel.setJnpfKey(FormEnum.table.getMessage());
                List<FieldsModel> childAll = new ArrayList<>();
                List<FormColumnModel> childList = model.getChildList().getChildList();
                for (FormColumnModel column : childList) {
                    FieldsModel fieLdsModel = column.getFieLdsModel();
                    model(fieLdsModel);
                    childAll.add(fieLdsModel);
                }
                configModel.setChildren(childAll);
                child.setConfig(configModel);
                fieldsAll.add(child);
            }
        }
        return fieldsAll;
    }

    /**
     * app日期赋默认属性
     * @param fieLdsModel
     */
    private void model(FieldsModel fieLdsModel) {
        String jnpfkey = fieLdsModel.getConfig().getJnpfKey();
        if (StringUtil.isNotEmpty(fieLdsModel.getVModel())) {
            if ("date".equals(jnpfkey) || "dateRange".equals(jnpfkey)) {
                fieLdsModel.setFormat("yyyy-MM-dd");
                fieLdsModel.setValueformat("timestamp");
            } else if ("timeRange".equals(jnpfkey) || "time".equals(jnpfkey)) {
                fieLdsModel.setFormat("HH:mm:ss");
                fieLdsModel.setValueformat("HH:mm:ss");
            }
        }
    }

}
