import { message } from 'antd'
import * as service from '../services/index'
import URLS from '../constants/url'
import moment from 'moment'

function getBodyFilter(modelFilter) {
    let { name, label, operator, operatorLabel, type, value1, value2 } = modelFilter;
    let bodyFilter = {
        columnName: name,
        columnLabel: label,
        columnType: type,
        symbol: operator,
        symbolLabel: operatorLabel,
        value: value1
    };
    if(type === 'scale' && operator === 'between') {
        bodyFilter['value'] = value1 + ',' + value2;
    }else if(type === 'time') {
        let v1 = value1.dynamic ? value1.name : moment(value1).format('YYYY-MM-DD');
        let v2 = value2.dynamic ? value2.name : moment(value2).format('YYYY-MM-DD');

        if(operator === 'between') {
            bodyFilter['value'] = v1 + ',' + v2;
        }else {
            bodyFilter['value'] = v1;
        }
    }else if(type === 'categorical' && (operator === 'contain' || operator === 'notContain')) {
        bodyFilter['value'] = JSON.stringify(value1);
    }
    return bodyFilter;
}
function getModelFilter(resFilter) {
    let { columnName, columnLabel, columnType, symbol, symbolLabel, value } = resFilter;
    let modelFilter = {
        name: columnName,
        label: columnLabel,
        type: columnType,
        operator: symbol,
        operatorLabel: symbolLabel,
        value1: value
    };
    if(columnType === 'scale' && symbol === 'between') {
        modelFilter['value1'] = value.split(',')[0];
        modelFilter['value2'] = value.split(',')[1];
    }else if(columnType === 'time') {
        let value1 = value.split(',')[0];
        let value2 = value.split(',')[1];
        let v1 = moment(value1).isValid() ? moment(value1).format('YYYY-MM-DD') : value1;
        let v2 = moment(value2).isValid() ? moment(value2).format('YYYY-MM-DD') : value2;

        if(symbol === 'between') {
            modelFilter['value1'] = v1;
            modelFilter['value2'] = v2;
        }else {
            modelFilter['value1'] = v1;
        }
    }else if(columnType === 'categorical' && (symbol === 'contain' || symbol === 'notContain')) {
        modelFilter['value1'] = JSON.parse(value);
    }
    return modelFilter;
}

export default {
    namespace: 'chartPolicy',
    state: {
        chartCode: null,
        columnData: [],
        list: [{
            code: '1',
                    enabled: true,
                    name: '开放策略1',
                    targets: [],
                    filters: []
        }]
    },
    reducers: {
        list(state, action) {
            const { list } = action;
            return { ...state, list };
        },
        modify(state, action) {
            const { policy } = action;
            let { list } = state;
            for(let i = 0; i < list.length; i++) {
                if(list[i].code === policy.code) {
                    list[i] = policy;
                    break;
                }
            }
            return { ...state, list };
        },
        setColumnData(state, action) {
            const { columnData } = action;
            return { ...state, columnData }
        }
    },
    effects: {
        *fetchList(action, { select, call, put }) {
            const body = action.chartCode;
            try {
                const res = yield call(service.fetch, {
                    url: URLS.CHART_POLICY_LIST,
                    body
                });
                if(res.code > 0) {
                    let list = (res.data && res.data.length > 0) ? res.data.sort((a, b) => new Date(a.createDate) - new Date(b.createDate)).map(d => ({
                        code: d.strategies.id,
                        enabled: d.strategies.isOpen+'' === '1',
                        name: d.strategies.name,
                        targets: d.userGroupName.map(g => ({
                            code: g.id+'',
                            isGroup: true,
                            name: g.name
                        })).concat(d.userName.map(u => ({
                            code: u.id+'',
                            isGroup: false,
                            name: u.name
                        }))),
                        filters: d.strategies.ruleStr ? JSON.parse(d.strategies.ruleStr).map(f => getModelFilter(f)) : [],
                    })) : [];
                    yield put({ type: 'list', list });
                }else {
                    message.error('请求图表策略列表失败: ' + res.msg);
                }
            }catch(e) {
                message.error('读取图表策略列表错误: ' + e.message);
            }
        },
        *remoteAdd(action, { put, call, select }) {
            const chartPolicy = yield select(state => state.chartPolicy);
            const { chartCode, policy } = action;
            const body = {
                type: 'chart',
                tarId: chartCode,
                name: policy.name,
                rule: policy.filters.map(f => getBodyFilter(f)),
                isOpen: policy.enabled ? '1' : '0',
            };
            try {
                const res = yield call(service.fetch, {
                    url: URLS.CHART_POLICY_ADD,
                    body,
                });
                if(res.code > 0) {
                    let { list } = chartPolicy;
                    list.push({
                        code: res.data,
                        ...policy
                    });
                    yield put({ type: 'list', list });
                }else {
                    message.error('添加失败: ' + res.msg);
                }
            }catch(e) {
                message.error('添加失败: ' + e.message);
            }
        },
        *remoteModify(action, { put, call, select }) {
            const { policy, chartCode } = action;
            const body = {
                id: policy.code,
                type: 'chart',
                tarId: chartCode,
                name: policy.name,
                rule: policy.filters.map(f => getBodyFilter(f)),
                isOpen: policy.enabled ? '1' : '0',
            };
            try {
                const res = yield call(service.fetch, {
                    url: URLS.CHART_POLICY_UPDATE,
                    body
                });
                if(res.code > 0) {
                    yield put({ type: 'modify', policy });
                }else {
                    message.error('修改失败: ' + res.msg);
                }
            }catch(e) {
                message.error('修改失败: ' + e.message);
            }
        },
        *remoteDelete(action, { put, call, select }) {
            const chartPolicy = yield select(state => state.chartPolicy);
            const { code } = action;
            const body = code;
            try {
                const res = yield call(service.fetch, {
                    url: URLS.CHART_POLICY_DELETE,
                    body
                });
                if(res.code > 0) {
                    let { list } = chartPolicy;
                    for(let i = 0; i < list.length; i++) {
                        if(list[i].code === code) {
                            list.splice(i, 1);
                            break;
                        }
                    }
                    yield put({ type: 'list', list });
                }else {
                    message.error('删除失败: ' + res.msg);
                }
            }catch(e) {
                message.error('删除失败: ' + e.message);
            }
        },
        *remoteSetTarget(action, { put, call, select }) {
            const { policy, targets } = action;
            const body = {
                stId: policy.code+'',
                type: 'chart',
                obj: targets.map(t => ({
                    obId: t.code,
                    objectType: t.isGroup ? '0' : '1'
                }))
            };
            try {
                const res = yield call(service.fetch, {
                    url: URLS.CHART_POLICY_TARGET_UPDATE,
                    body
                });
                if(res.code > 0) {
                    yield put({ type: 'modify', policy: {
                        ...policy,
                        targets
                    } });
                }else {
                    message.error('设置对象失败: ' + res.msg);
                }
            }catch(e) {
                message.error('设置对象失败: ' + e.message);
            }
        },
        /**
         * 获得列数据
         */
        *remoteColumnData(action, { put, call, select }) {
            const body = action.chartCode;
            try {
                const res = yield call(service.fetch, {
                    url: URLS.CHART_QUERY_DATACOLUMNS,
                    body
                });
                if(res.code > 0) {
                    let columnData = res.data.map(d => ({
                        name: d.columnName,
                        label: d.columnRaname,
                        type: d.columnType,
                    }));
                    yield put({ type: 'setColumnData', columnData });
                }else {
                    message.error('获得图表列数据失败: ' + res.msg);
                }
            }catch(e) {
                message.error('获得图表列数据失败: ' + e.message);
            }
        }
    }
};