/**
 * Component - Explorer Component
 *
 * @file Explorer.js
 * @author mudio(job.mudio@gmail.com)
 */

import path from 'path';
import {remote} from 'electron';
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import {Modal, notification} from 'antd';

import Url from './Url';
import Window from './Window';
import Migration from './migration/migration';
import styles from './Explorer.css';
import SideBar from '../App/SideBar';

import {
    MENU_COPY_COMMAND,
    MENU_MOVE_COMMAND,
    MENU_TRASH_COMMAND,
    MENU_RENAME_COMMAND,
    MENU_DOWNLOAD_COMMAND
} from '../../actions/context';

import {redirect, trash, migration} from '../../actions/explorer';
import {createDownloadTask} from '../../actions/downloader';

export default class Explorer extends Component {
    static propTypes = {
        nav: PropTypes.shape({
            region: PropTypes.string.isRequired,
            bucket: PropTypes.string,
            prefix: PropTypes.string
        }),
        dispatch: PropTypes.func.isRequired
    };

    state = {
        visible: false,
        option: {}
    }

    componentDidMount() {
        const {dispatch, nav} = this.props;
        dispatch(redirect(nav.region, nav.bucket, nav.prefix));
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.nav.region !== this.props.nav.region) {
            redirect(nextProps.nav.region);
        }
    }

    _onCommand(cmd, config) {
        const {region, bucketName, prefix, keys} = config;

        switch (cmd) {
        case MENU_MOVE_COMMAND:
        case MENU_COPY_COMMAND:
        case MENU_RENAME_COMMAND: {
            return this.setState({
                visible: true,
                option: {region, bucket: bucketName, object: keys[0], command: cmd}
            });
        }
        case MENU_TRASH_COMMAND:
            return this._trash(region, bucketName, prefix, keys);
        case MENU_DOWNLOAD_COMMAND:
            return this._download(region, bucketName, prefix, keys);
        default:
        }
    }

    _download(region, bucketName, prefix, keys) {
        // 选择文件夹
        const selectPaths = remote.dialog.showOpenDialog({properties: ['openDirectory']});
        // 用户取消了
        if (selectPaths === undefined) {
            return;
        }

        // 不支持选择多个文件夹，所以只取第一个
        const dirname = prefix.endsWith('/') ? prefix : path.posix.dirname(prefix);
        this.props.dispatch(
            createDownloadTask(region, bucketName, dirname, keys, selectPaths[0])
        );
    }

    _trash(region, bucketName, prefix, keys) {
        Modal.confirm({
            title: '删除提示',
            content: `您确定删除${keys.length}个文件吗?`,
            onOk: () => this.props.dispatch(
                trash(region, bucketName, prefix, keys)
            ).then(
                () => notification.success({message: '删除成功', description: `成功删除${keys.length}个文件`})
            )
        });
    }

    _onCancel = () => {
        this.setState({visible: false});
    }

    _onMigration = (config = {}, removeSource = false) => {
        const {
            sourceBucket, sourceObject,
            targetBucket, targetObject
        } = config;

        this.setState({visible: false});

        if (sourceBucket !== targetBucket || sourceObject !== targetObject) {
            this.props.dispatch(
                migration(config, removeSource)
            ).then(res => {
                if (res.error) {
                    notification.error(
                        {message: '操作失败', description: `${res.error} => ${sourceObject}`}
                    );
                } else {
                    notification.success(
                        {message: '操作成功', description: `${sourceObject} => ${targetObject}`}
                    );
                }
            });
        }
    }

    updateValue(evt) {
        const target = evt.target.value;
        const option = this.state.option;

        option.target = target;

        this.setState({option});
    }

    render() {
        const {nav, dispatch} = this.props;
        const {visible, option} = this.state;

        return (
            <div className={styles.container}>
                <SideBar />
                <div className={styles.body}>
                    <Url redirect={(...args) => dispatch(redirect(...args))} />
                    <Window nav={nav}
                        onCommand={(...args) => this._onCommand(...args)}
                        redirect={(...args) => dispatch(redirect(...args))}
                    />
                </div>
                <Migration {...option}
                    visible={visible}
                    onMigration={this._onMigration}
                    onCancel={this._onCancel}
                />
            </div>
        );
    }
}

@@ -3402,24 +3402,143 @@
 en(res =%3E %7B%0A
+                const %7BerrorCount, successCount%7D = res;%0A                const totalCount = errorCount + successCount;%0A%0A
             
@@ -3554,16 +3554,25 @@
 es.error
+Count %3E 0
 ) %7B%0A    
@@ -3606,16 +3606,17 @@
 n.error(
+%7B
 %0A       
@@ -3624,33 +3624,32 @@
                 
-%7B
 message: '%E6%93%8D%E4%BD%9C%E5%A4%B1%E8%B4%A5',
@@ -3648,12 +3648,37 @@
  '%E6%93%8D%E4%BD%9C
-%E5%A4%B1%E8%B4%A5',
+%E6%9C%AA%E5%AE%8C%E6%88%90',%0A                       
  des
@@ -3694,39 +3694,78 @@
  %60$%7B
-res.error
+sourceObject
 %7D =%3E $%7B
-source
+target
 Object%7D
+, %E5%85%B1%E8%AE%A1$%7BtotalCount%7D%E4%B8%AA%E6%96%87%E4%BB%B6%EF%BC%8C%E5%87%BA%E9%94%99$%7BerrorCount%7D%E4%B8%AA
 %60
-%7D
 %0A   
@@ -3773,32 +3773,33 @@
                 
+%7D
 );%0A             
@@ -3851,16 +3851,17 @@
 success(
+%7B
 %0A       
@@ -3877,17 +3877,16 @@
         
-%7B
 message:
@@ -3889,24 +3889,48 @@
 age: '%E6%93%8D%E4%BD%9C%E6%88%90%E5%8A%9F',
+%0A                       
  description
@@ -3966,18 +3966,36 @@
 tObject%7D
+%EF%BC%8C%E5%85%B1%E8%AE%A1$%7BtotalCount%7D%E4%B8%AA%E6%96%87%E4%BB%B6
 %60
-%7D
 %0A       
@@ -4003,24 +4003,25 @@
             
+%7D
 );%0A         

fix CopyObject不能展示错误