import React from 'react';
import path from 'path';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import { MenuBar } from './menubar';
import { Icon, NgIf, Loader, EventEmitter, EventReceiver } from '../../components/';
import { alert } from '../../helpers/';
import { Pager } from './pager';
import { SmallExif, LargeExif } from './image_exif';
import './imageviewer.scss';
import './pager.scss';
@EventReceiver
@EventEmitter
export class ImageViewer extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            preload: null,
            _: null,
            show_exif: false,
            is_loaded: false
        };
        this.shortcut= (e) => {
            if(e.keyCode === 27){ this.setState({show_exif: false}); }
            else if(e.keyCode === 73){ this.setState({show_exif: !this.state.show_exif}); }
        };
        this.refresh = () => { this.setState({"_": Math.random()}); };
    }
    componentDidMount(){
        this.props.subscribe("media::preload", (preload) => {
            this.setState({preload: preload});
        });
        document.addEventListener('webkitfullscreenchange', this.refresh);
        document.addEventListener('mozfullscreenchange', this.refresh);
        document.addEventListener('fullscreenchange', this.refresh);
        document.addEventListener('keydown', this.shortcut);
    }
    componentWillUnmount(){
        this.props.unsubscribe("media::preload");
        document.removeEventListener('webkitfullscreenchange', this.refresh);
        document.removeEventListener('mozfullscreenchange', this.refresh);
        document.removeEventListener('fullscreenchange', this.refresh);
        document.removeEventListener('keydown', this.shortcut);
    }
    componentWillReceiveProps(props){
        if(props.data !== this.props.data){
            this.setState({is_loaded: false});
        }
    }
    toggleExif(){
        if(window.innerWidth < 580){
            alert.now();
        }else{
            this.setState({
                show_exif: !this.state.show_exif
            });
        }
    }
    requestFullScreen(){
        if("webkitRequestFullscreen" in document.body){
            this.refs.$container.webkitRequestFullscreen();
        }else if("mozRequestFullScreen" in document.body){
            this.refs.$container.mozRequestFullScreen();
        }
    }
    render(){
        const hasExif = (filename) => {
            const ext = path.extname(filename).toLowerCase().substring(1);
            return ["jpg", "jpeg", "tiff", "tif"].indexOf(ext) !== -1;
        };
        return (
            
              
                
                  
                
                
                  
                
              
              
                
                   this.setState({is_loaded: true})} url={this.props.data} />
                
                
                 this.setState({preload: e})} />
               
              
                
              
             
        );
    }
}
@EventEmitter
class ImageFancy extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            isLoading: true,
            isError: false,
            drag_init: {x: null, t: null},
            drag_current: {x: null, t: null},
            hasAction: false
        };
        this.img = new Image();
        this.img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=';
    }
    componentWillReceiveProps(nextProp){
        if(nextProp.url !== this.props.url){
            this.setState({
                isLoading: true,
                isError: false,
                drag_current: {x: 0},
                hasAction: false
            });
        }
    }
    onLoad(){
        this.setState({isLoading: false});
        this.props.onLoad();
    }
    onError(w){
        this.setState({isError: true});
    }
    imageDragStart(e){
        const t = new Date();
        if(e.touches){
            this.setState({
                drag_init: {x: e.touches[0].clientX, t: t},
                hasAction: true
            });
        }else{
            this.setState({
                drag_init: {x: e.pageX, t: t},
                hasAction: true
            });
        }
        if(e.dataTransfer) e.dataTransfer.setDragImage(this.img, 0, 0);
    }
    imageDragEnd(e){
        const drag_end = {
            x: function(dragX, touch){
                const t = new Date();
                if(dragX !== null) return dragX;
                if(touch && touch[0]){
                    return touch[0].clientX;
                }
                return 0;
            }(e.pageX || null, e.changedTouches || null),
            t: new Date()
        };
        const direction = function(x_current, x_init){
            if(x_current.t - x_init.t > 200){
                if(Math.abs(x_current.x - x_init.x) < (window.innerWidth < 500 ? window.innerWidth / 3 : 250)) return "neutral";
            }
            return x_current.x > x_init.x ? "right" : "left";
        }(drag_end, this.state.drag_init);
        if(direction === "left"){
            return this.setState({
                drag_current: {x: - window.innerWidth},
                hasAction: false
            }, () => {
                this.props.emit("media::next");
            });
        }else if(direction === "right"){
            return this.setState({
                drag_current: {x: + window.innerWidth},
                hasAction: false
            }, () => {
                this.props.emit("media::previous");
            });
        }
        return this.setState({
            drag_current: {x: 0},
            hasAction: false
        });
    }
    imageDrag(e){
        if(e.pageX > 0){
            this.setState({drag_current: {x: e.pageX - this.state.drag_init.x}});
        }else if(e.touches && e.touches[0].clientX > 0){
            this.setState({drag_current: {x: e.touches[0].clientX - this.state.drag_init.x}});
        }
    }
    render(){
        if(this.state.isError){
            return (
                
                  
                
            );
        }
        if(this.state.isLoading){
            return (
                
            );
        }
        return (
            
              
            
        );
    }
}
class Img extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        const image_url = (url, size) => {
            return url+"&meta=true&size="+parseInt(window.innerWidth*size);
        };
        if(!this.props.src) return null;
        return (
            
        );
    }
}