import React from "react";
import api from "./api/api";
import axios from "axios";
import cookie from "react-cookies";
import CircularProgress from "@material-ui/core/CircularProgress";
import Countdown from "react-countdown-now";
import Drawer from "./drawer";
import Dialog from "material-ui/Dialog";
import FlatButton from "material-ui/FlatButton";
class freebieDrawer extends React.Component {
  constructor(props) {
    super(props);
    console.log("session", localStorage.getItem("current_state"));
    console.log("is_active", localStorage.getItem("login_is_active"));
    if (
       localStorage.getItem("current_state") !== "freebie_drawer")  {
        this.props.history.push("/menu_page");
      }
    // let contest_id = localStorage.getItem("contest_id");
    // console.log("contest_id", contest_id);
    let user_id = cookie.load("user_id");
    console.log("user_id", user_id);
    this.state = {
        loader_freebie: false,
      user_id: user_id,
      open: false,
      alertmsg: "",
      contest: [],
      freebies:[],
      is_active: localStorage.getItem("login_is_active") == 1 ? true : false,
      push_contest: false
    };
  }
  componentDidMount() {
    console.log("here");
    var getfinaljson = {
      action: "getcontest",
      user_id: cookie.load("user_id"),
      is_active: 1
    };
    var self = this;
    self.setState({ loader_freebie: true });
    console.log("getfinaljson", getfinaljson);
    axios
      .request({
        method: "POST",
        url: api.react_request,
        data: JSON.stringify(getfinaljson)
      })
      .then(response => {
        console.log("response", response);
        if (response.data) {
          if (response.data.length !== 0) {
            self.setState({ loader_freebie: false });
            localStorage.setItem("is_contest", response.data[0].is_contest);
            console.log(
              "response.data[0].is_contest",
              response.data[0].is_contest
            );
            let freebies = [];
            response.data.forEach(element => {
              console.log("element", element);
              if (element.is_contest == 0) {
                freebies.push(element);
              }
            });
            localStorage.setItem("current_state", "contest");
            localStorage.setItem(
              "contest_array",
              JSON.stringify(freebies)
            );
            localStorage.setItem(
              "contest_detail",
              JSON.stringify(freebies[0])
            );
            localStorage.setItem("contest_index", 0);
            console.log("contest", freebies);
            if (freebies.length === 0) {
              console.log("freebies.length", freebies.length);
              self.setState({ loader_freebie: false });
              localStorage.setItem("current_state", "no_freebie"),
            self.props.history.push("/no_freebie");
            } else {
              console.log("freebies.length", freebies.length);
              self.setState({ loader_freebie: false });
              localStorage.setItem("current_state", "contest"),
              self.props.history.push("/Contest/" +
                freebies[0]._id +
                  "/" +
                  cookie.load("user_id")
              );
            }
          } else {
            self.setState({ loader_freebie: false });
            localStorage.setItem("current_state", "no_freebie"),
            self.props.history.push("/no_freebie");
          }
        } else {
          self.setState({ loader_freebie: false });
          localStorage.setItem("current_state", "no_freebie"),
          self.props.history.push("/no_freebie");
        }
      })
      .catch(error => {
        console.log("error", error);
        self.setState({ loader_freebie: false });
        self.setState({
          nocontest: false,
          open: true,
          alertmsg: "Something wrong... Please try again later"
        });
      });
  }

  handleClose = () => {
    this.setState({ open: false });
  };

  render() {
    // const { open } = this.state;
    return (
      <div>
        <center>
          {this.state.is_active && <Drawer prop={this.props} />}
          {this.state.loader_freebie && (
            <center>
              <CircularProgress
                size={100}
                style={{ color: "black", marginTop: "17%" }}
              />
            </center>
          )}
          <Dialog
            style={{ textTransform: "capitalize" }}
            actions={
              <FlatButton
                label="OK"
                labelStyle={{ color: "#000" }}
                onClick={() => this.handleClose()}
              />
            }
            modal={false}
            open={this.state.open}
            //onRequestClose={this.handleClose}
          >
            {this.state.alertmsg}
          </Dialog>
          
        </center>
      </div>
    );
  }
}

export default freebieDrawer;
