import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import './style.scss';
import {Button} from "../../index";

const COOKIE_KEY = 'accept_cookie_policy';

class CookieConsent extends PureComponent {

    componentWillMount() {
        this.setState({
            show: !window.localStorage.getItem(COOKIE_KEY),
        });
    }

    handleClick() {
        window.localStorage.setItem(COOKIE_KEY, true);
        this.setState({
            show: false,
        });
    }

    render() {
        return this.state.show
            ? (
                <div className="CookieConsent">
                    <div className="CookieConsent__title">
                        <b>{this.props.title}</b>
                    </div>

                    <div
                        className="CookieConsent__message"
                        dangerouslySetInnerHTML={{__html: this.props.message}}
                    />
                    <div className="CookieConsent__actions">
                        <button
                            className="button button_primary"
                            onClick={() => this.handleClick()}
                        >
                        Got it
                        </button>
                    </div>
                </div>
            )
            : '';
    }
}

CookieConsent.propTypes = {
    title: PropTypes.string,
    message: PropTypes.string,
};

CookieConsent.defaultProps = {
    title: 'Cookies',
    message: 'This Website uses cookies to offer and ensure better browsing experience. Find out more on how we use cookies and how you can change your settings <a href="https://propellerads.com/cookies/" target="_blank">here</a>.',
};

export default CookieConsent;
