/*
Copyright 2017 New Vector Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from 'react';
import {MatrixEvent, MatrixClient} from 'matrix-js-sdk';
import sdk from '../../../index';
import { _t } from '../../../languageHandler';
import Modal from '../../../Modal';

const GROUP_ID_REGEX = /\+\S+\:\S+/;

module.exports = React.createClass({
    displayName: 'RelatedGroupSettings',

    propTypes: {
        roomId: React.PropTypes.string.isRequired,
        canSetRelatedGroups: React.PropTypes.bool.isRequired,
        relatedGroupsEvent: React.PropTypes.instanceOf(MatrixEvent),
    },

    contextTypes: {
        matrixClient: React.PropTypes.instanceOf(MatrixClient),
    },

    getDefaultProps: function() {
        return {
            canSetRelatedGroups: false,
        };
    },

    getInitialState: function() {
        return {
            newGroupsList: this.props.relatedGroupsEvent ?
                (this.props.relatedGroupsEvent.getContent().groups || []) : [],
            hasChanged: false,
            newGroupId: null,
        };
    },

    saveSettings: function() {
        if (!this.state.hasChanged) return Promise.resolve();

        return this.context.matrixClient.sendStateEvent(
            this.props.roomId,
            'm.room.related_groups',
            {
                groups: this.state.newGroupsList,
            },
            '',
        );
    },

    validateGroupId: function(groupId) {
        if (!GROUP_ID_REGEX.test(groupId)) {
            const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
            Modal.createTrackedDialog('Invalid related community ID', '', ErrorDialog, {
                title: _t('Invalid community ID'),
                description: _t('\'%(groupId)s\' is not a valid community ID', { groupId }),
            });
            return false;
        }
        return true;
    },

    onNewGroupChanged: function(newGroupId) {
        this.setState({ newGroupId });
    },

    onGroupAdded: function(groupId) {
        if (groupId.length === 0 || !this.validateGroupId(groupId)) {
            return;
        }
        this.setState({
            newGroupsList: this.state.newGroupsList.concat([groupId]),
            hasChanged: true,
            newGroupId: '',
        });
    },

    onGroupEdited: function(groupId, index) {
        if (groupId.length === 0 || !this.validateGroupId(groupId)) {
            return;
        }
        this.setState({
            newGroupsList: Object.assign(this.state.newGroupsList, {[index]: groupId}),
            hasChanged: true,
        });
    },

    onGroupDeleted: function(index) {
        const newGroupsList = this.state.newGroupsList.slice();
        newGroupsList.splice(index, 1);
        this.setState({ newGroupsList, hasChanged: true });
    },

    render: function() {
        const localDomain = this.context.matrixClient.getDomain();
        const EditableItemList = sdk.getComponent('elements.EditableItemList');
        return <div>
            <h3>{ _t('Flair') }</h3>
            <EditableItemList
                items={this.state.newGroupsList}
                className={"mx_RelatedGroupSettings"}
                newItem={this.state.newGroupId}
                canEdit={this.props.canSetRelatedGroups}
                onNewItemChanged={this.onNewGroupChanged}
                onItemAdded={this.onGroupAdded}
                onItemEdited={this.onGroupEdited}
                onItemRemoved={this.onGroupDeleted}
                itemsLabel={_t('Showing flair for these communities:')}
                noItemsLabel={_t('This room is not showing flair for any communities')}
                placeholder={_t(
                    'New community ID (e.g. +foo:%(localDomain)s)', {localDomain},
                )}
            />
        </div>;
    },
});

@@ -756,16 +756,54 @@
 /Modal';
+%0Aimport isEqual from 'lodash/isEqual';
 %0A%0Aconst 
@@ -1401,24 +1401,154 @@
 sList: this.
+getInitialGroupList(),%0A            newGroupId: null,%0A        %7D;%0A    %7D,%0A%0A    getInitialGroupList: function() %7B%0A        return this.
 props.relate
@@ -1561,32 +1561,16 @@
 sEvent ?
-%0A               
  (this.p
@@ -1628,80 +1628,330 @@
 : %5B%5D
+;%0A    %7D
 ,%0A
+%0A
     
-        hasChanged: false,%0A            newGroupId: null,%0A        %7D
+needsSaving: function() %7B%0A        const cli = this.context.matrixClient;%0A        const room = cli.getRoom(this.props.roomId);%0A        if (!room.currentState.maySendStateEvent('m.room.related_groups', cli.getUserId())) return false;%0A        return isEqual(this.getInitialGroupList(), this.state.newGroupsList)
 ;%0A  
@@ -2009,24 +2009,21 @@
 his.
-state.hasChanged
+needsSaving()
 ) re
@@ -3089,38 +3089,8 @@
 %5D),%0A
-            hasChanged: true,%0A
     
@@ -3395,38 +3395,8 @@
 %7D),%0A
-            hasChanged: true,%0A
     
@@ -3594,26 +3594,8 @@
 List
-, hasChanged: true
  %7D);

check PL and compare arrays rather than tracking changes