/**
 * @file CheckboxGroup component
 * @author liangxiaojun(liangxiaojun@derbysoft.com)
 */

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';

import Checkbox from '../Checkbox';
import Theme from '../Theme';

import Util from '../_vendors/Util';

export default class CheckboxGroup extends Component {

    static Theme = Theme;

    constructor(props, ...restArgs) {

        super(props, ...restArgs);

        this.state = {
            value: props.value
        };

        this.changeHandler = ::this.changeHandler;

    }

    changeHandler(item) {

        let value = _.cloneDeep(this.state.value);

        if (!value || !_.isArray(value)) {
            value = [item];
        } else {

            const index = value.findIndex(v => v.value === item.value);

            if (index > -1) {
                value.splice(index, 1);
            } else {
                value.push(item);
            }

        }

        this.setState({
            value
        }, () => {
            const {disabled, onChange} = this.props;
            !disabled && onChange && onChange(value);
        });

    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.value && nextProps.value !== this.state.value) {
            this.setState({
                value: nextProps.value
            });
        }
    }

    render() {

        const {data} = this.props;

        if (!data) {
            return null;
        }

        const {
                className, style, theme, name, disabled, idProp, uncheckedIconCls, checkedIconCls, indeterminateIconCls,
                onCheck, onUncheck
            } = this.props,
            {value} = this.state;

        return (
            <div className={'checkbox-group' + (className ? ' ' + className : '')}
                 style={style}
                 disabled={disabled}>

                {
                    data.map((item, index) => {

                        const isChecked = value && value.findIndex(v => v.value === item.value) > -1;

                        return (
                            <Checkbox key={idProp in item ? item[idProp] : index}
                                      className={item.className ? item.className : ''}
                                      style={item.style}
                                      theme={item.theme || theme}
                                      name={name}
                                      label={item.label}
                                      value={item.value}
                                      disabled={disabled || item.disabled}
                                      checked={isChecked}
                                      uncheckedIconCls={item.uncheckedIconCls || uncheckedIconCls}
                                      checkedIconCls={item.checkedIconCls || checkedIconCls}
                                      indeterminateIconCls={item.indeterminateIconCls || indeterminateIconCls}
                                      onChange={() => {
                                          this.changeHandler(item);
                                      }}
                                      onCheck={() => {
                                          onCheck && onCheck(item);
                                      }}
                                      onUncheck={() => {
                                          onUncheck && onUncheck(item);
                                      }}/>
                        );

                    })
                }

            </div>
        );

    }
};

CheckboxGroup.propTypes = {

    className: PropTypes.string,
    style: PropTypes.object,

    /**
     * The CheckboxGroup theme.
     */
    theme: PropTypes.oneOf(Util.enumerateValue(Theme)),

    name: PropTypes.string,
    data: PropTypes.arrayOf(PropTypes.shape({

        className: PropTypes.string,
        style: PropTypes.object,
        theme: PropTypes.oneOf(Util.enumerateValue(Theme)),

        label: PropTypes.any,
        value: PropTypes.any

    })).isRequired,
    value: PropTypes.array,
    disabled: PropTypes.bool,

    idProp: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),

    uncheckedIconCls: PropTypes.string,
    checkedIconCls: PropTypes.string,
    indeterminateIconCls: PropTypes.string,

    onChange: PropTypes.func,

    onCheck: PropTypes.func,

    onUncheck: PropTypes.func

};

CheckboxGroup.defaultProps = {

    className: null,
    style: null,
    theme: Theme.DEFAULT,

    name: null,
    data: null,
    value: null,
    disabled: false,
    idProp: 'id',

    uncheckedIconCls: 'fa fa-square-o',
    checkedIconCls: 'fa fa-check-square',
    indeterminateIconCls: 'fa fa-minus-square'

};
@@ -291,16 +291,61 @@
 s/Util';
+%0Aimport Position from '../_statics/Position';
 %0A%0Aexport
@@ -2466,207 +2466,378 @@
     
-name=%7Bname%7D%0A                                      label=%7Bitem.label%7D%0A                                      value=%7Bitem.value%7D%0A                                      disabled=%7Bdisabled %7C%7C item.disabled
+uncheckedIconCls=%7Bitem.uncheckedIconCls %7C%7C uncheckedIconCls%7D%0A                                      checkedIconCls=%7Bitem.checkedIconCls %7C%7C checkedIconCls%7D%0A                                      indeterminateIconCls=%7Bitem.indeterminateIconCls %7C%7C indeterminateIconCls%7D%0A                                      name=%7Bname%7D%0A                                      label=%7Bitem.label
 %7D%0A  
@@ -2876,26 +2876,25 @@
     
-checked=%7BisChecked
+value=%7Bitem.value
 %7D%0A  
@@ -2933,271 +2933,223 @@
     
-uncheckedIconCls=%7Bitem.uncheckedIconCls %7C%7C uncheckedIconCls%7D%0A                                      checkedIconCls=%7Bitem.checkedIconCls %7C%7C checkedIconCls%7D%0A                                      indeterminateIconCls=%7Bitem.indeterminateIconCls %7C%7C indeterminateIconCls
+disabled=%7Bdisabled %7C%7C item.disabled%7D%0A                                      checked=%7BisChecked%7D%0A                                      tip=%7Bitem.tip%7D%0A                                      tipPosition=%7Bitem.tipPosition
 %7D%0A  
@@ -3979,24 +3979,25 @@
 pes.string,%0A
+%0A
     data: Pr
@@ -4220,16 +4220,149 @@
 ypes.any
+,%0A%0A        disabled: PropTypes.bool,%0A%0A        tip: PropTypes.any,%0A        tipPosition: PropTypes.oneOf(Util.enumerateValue(Position))
 %0A%0A    %7D)

update CheckboxGroup