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 contestDrawer 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") !== "contest_drawer" ||
    //   cookie.load("user_id") == null ||
    //   cookie.load("user_id") == "null" ||
    //   cookie.load("user_id") == "" ||
    //   cookie.load("user_id") == "undefined" ||
    //   typeof cookie.load("user_id") == undefined
    // ) {
    //   console.log("session", cookie.load("user_id"));
    //   this.props.history.push("/");
    // }
    let user_id = cookie.load("user_id");
    console.log("user_id", user_id);
    this.state = {
      loader: 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: localStorage.getItem("user_id"),
      is_active: 1
    };
    var self = this;
    self.setState({ loader: 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: false });
            localStorage.setItem("is_contest", response.data[0].is_contest);
            console.log(
              "response.data[0].is_contest",
              response.data[0].is_contest
            );
            let contest = [];
            response.data.forEach(element => {
              console.log("element", element);
              if (element.is_contest == 1) {
                contest.push(element);
              }
            });
            localStorage.setItem("current_state", "contest");
            localStorage.setItem(
              "contest_array",
              JSON.stringify(contest)
            );
            localStorage.setItem(
              "contest_detail",
              JSON.stringify(contest[0])
            );
            localStorage.setItem("contest_index", 0);
            console.log("contest", contest);
            if (contest.length === 0) {
              self.setState({ loader: false });
              localStorage.setItem("current_state", "no_contest"),
            self.props.history.push("/no_contest");
            } else {
              self.setState({ loader: false });
              console.log("contest", contest)
              if(typeof(localStorage.getItem("need_to_claim"))!== undefined && localStorage.getItem("need_to_claim") !== "undefined" && localStorage.getItem("need_to_claim")!=="null" && localStorage.getItem("need_to_claim")!==null){
                console.log(JSON.parse(localStorage.getItem("need_to_claim")))
                localStorage.setItem("current_state", "won"),
                localStorage.setItem("need_to_claim_detail",JSON.stringify(JSON.parse(localStorage.getItem("need_to_claim"))[0]))
                self.props.history.push("/won")
              }
              else{
                localStorage.setItem("current_state", "contest"),
                self.props.history.push(
                  "/Contest/" +
                  contest[0]._id +
                    "/" +
                    cookie.load("user_id")
                );
              }
              
            }
          } else {
            self.setState({ loader: false });
            localStorage.setItem("current_state", "no_contest"),
            self.props.history.push("/no_contest");
          }
        } else {
          self.setState({ loader: false });
          localStorage.setItem("current_state", "no_contest"),
          self.props.history.push("/no_contest");
        }
      })
      .catch(error => {
        console.log("error", error);
        self.setState({ loader: false });
        self.setState({
          nocontest: false,
          dialogopen: 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 && (
            <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 contestDrawer;
