/**
 * Created by rburson on 1/15/16.
 */

import * as React from 'react'
import {CvState, CvProps, CvBaseMixin} from './catreact-core'
import {AppContext} from 'catavolt-sdk'

export interface CvResourceState extends CvState {
}

export interface CvResourceProps extends CvProps {
    resourceName?:string
    resourceUrl?:string;
    resourceRenderer:(resourceUrl:string)=>{}
}

export var CvResource = React.createClass<CvResourceProps, CvResourceState>({

    mixins: [CvBaseMixin],

    getDefaultProps: function () {
        return {
            resourceName: null,
            resourceUrl: null,
            resourceRenderer:null
        }
    },

    render: function () {
        if (this.props.resourceName || this.props.resourceUrl) {
            let url = this.props.resourceUrl;
            if (this.props.resourceName) {
                let baseUrl = null;
                const tenantSettingsTry = (this.catavolt() as AppContext).tenantSettingsTry;
                if (tenantSettingsTry.isSuccess) {
                    baseUrl = tenantSettingsTry.success['GMLAssetsURL']
                }
                url = baseUrl + this.props.resourceName;
            }
            return this.props.resourceRenderer ? this.props.resourceRenderer(url) : null;
        } else {
            return null;
        }
    },

});