/*Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana

This file is part of Plataforma Integrada MEC.

Plataforma Integrada MEC is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Plataforma Integrada MEC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with Plataforma Integrada MEC.  If not, see <http://www.gnu.org/licenses/>.*/

import React, {useState, useEffect, useContext} from 'react';
import {Container} from 'react-grid-system';
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
import CardActionArea from '@material-ui/core/CardActionArea';
import CardActions from '@material-ui/core/CardActions';
import {apiDomain} from '../env';
import { Store } from '../Store.js';
import noAvatar from "../img/default_profile.png";
import { makeStyles } from '@material-ui/core/styles';
import CardMedia from '@material-ui/core/CardMedia';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import styled from 'styled-components'
import axios from 'axios'
import {apiUrl} from '../env';
import Options from './ContactCardOptions.js'
import FollowButton from './ContactButtons/FollowButton.js'
import FollowingButton from './ContactButtons/FollowingButton.js'
import FollowersCountButton from './ContactButtons/FollowersCountButton.js'
import {Link} from 'react-router-dom';

const useStyles = makeStyles({
  root: {
    maxWidth: 345,
    borderRadius : 0
  },
});

export default function ImgMediaCard(props) {
  const classes = useStyles();
  const {state} = useContext(Store)

  const [coverStyle, setCoverStyle] = useState({})

  const createRequestHeader = () => {
      const header = {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'Access-Token': state.currentUser.accessToken,
          'Client': state.currentUser.clientToken,
          'Uid': sessionStorage.getItem('@portalmec/uid'),
          'Host': 'api.portalmec.c3sl.ufpr.br',
          'Cookie': ''
      }
      return header
  }

  return (
        <StyledCard>
            <CardDiv>
                <CardAreaDiv>
                    {/*Top part of contat card (background image, number of followers and avatar)*/}
                    <Header>
                        <StyledCardMedia image={props.cover}>
                            <div style = {{display : "flex", backgroundColor : "inherit", float : "right"}}>
                                <Link to={props.href}>
                                    <FollowersCountButton followCount={props.follow_count}/>
                                </Link>
                                <AvatarDiv>
                                    <img src={props.avatar ? props.avatar : noAvatar} alt='user avatar'
                                        style={{height : "100%", width : "100%", borderRadius : "50%"}}/>
                                </AvatarDiv>
                            </div>
                        </StyledCardMedia>
                    </Header>

                    {/*Rest of the card content. Button to be rendered depends on whether the contact is followed by the user*/}
                    <CardContent>
                        <UserInfo>
                            <Link to={props.href}>
                                <p className="p1">
                                    {props.name}
                                </p>
                            </Link>

                            <Link to={props.href}>
                                <span style={{fontSize : "14px", fontWeight : "normal"}}>
                                    <b>{props.numCollections}</b> {props.numCollections != 1? "Coleções" : "Coleção"} | <b>{props.numLearningObjects}</b> {props.numLearningObjects != 1? "Recursos" : "Recurso"}
                                </span>
                            </Link>

                            <div style={{display : "flex", justifyContent : "center"}}>
                                {
                                    props.followed ?
                                    (
                                        [
                                            <FollowingButton followedID={props.followerID}/>
                                        ]
                                    )
                                    :
                                    (
                                        [
                                            <FollowButton followerID={props.followerID}/>
                                        ]
                                    )
                                }
                                <Options/> {/*options menu missing functionalities*/}
                            </div>
                    </UserInfo>
                </CardContent>
            </CardAreaDiv>
        </CardDiv>
    </StyledCard>
  );
}

/*Controls top part of Card*/
const Header = styled.div`
    display : flex;
    height : 152px;
    position : relative;
`

/* Had to create these classes so I could avoid using card action Area -------*/
export const CardAreaDiv = styled.div`
    display : flex;
    flex-direction : column;
    height : 360px;
    width : 272.5px;
    margin : 0 auto;
`
export const CardDiv = styled.div`
    background-color : #fff;
    text-align : start;
    font-family : 'Roboto', sans serif;
    color : #666;
`
/*----------------------------------------------------------------------------*/



/*Override Material UI styling -----------------------------------------------*/
const StyledCardMedia = styled(CardMedia) `
    height : 100%;
    width : 100%;
    background-size : cover;
    background-position : center;

`
const StyledCard = styled(Card)`
    width : 272.5px;
    max-height : 380px;
    margin-top : 10px;
    margin-bottom : 10px;
    max-width : 345px;
    border-radius : 0;
    box-shadow : 0 0 5px 0 rgba(0,0,0,.25) !important;
`
/*----------------------------------------------------------------------------*/


const UserInfo = styled.div`
    text-align : center;
    margin-top : 50px;
    color : #666;

    a {
        text-decoration : none !important;
        color : #666;
    }

    .p1 {
        font-size : 17px;
        margin : 0 0 10px;
        overflow : hidden;
        text-overflow : ellipsis;
        white-space : nowrap;
    }
`

/*Rounded div to be used with avatar pic*/
const AvatarDiv = styled.div`
    border-radius : 100%;
    left : 50%;
    position : absolute;
    -webkit-transform : translateX(-50%);
    transform : translateX(-50%);
    top : 65px;
    height : 126px;
    width : 126px;
    border : 2px solid #fff;
`