import React, { Component } from "react";
import FlatButton from "material-ui/FlatButton";
import Dialog from "material-ui/Dialog";
import { slideInRight } from "react-animations";
import Radium, { StyleRoot } from "radium";
import axios from "axios";
import CircularProgress from "@material-ui/core/CircularProgress";
import isMobile from "react-device-detect";
import api from "./api/api";
import cookie from "react-cookies";
let path;
var jwt_decode = require("jwt-decode");
var base64 = require("base-64");
class Home extends Component {
  constructor(props) {
    super(props);
    let is_contest = localStorage.getItem("contest");
    // localStorage.setItem("refered_by_id","email|5b507ac9a6803a9191926620")
    // localStorage.setItem("refered_contest_id","15")
    console.log("refered_by_id", cookie.load("refered_by_id"));
    console.log("refered_contest_id", cookie.load("refered_contest_id"));
    console.log("user_id", cookie.load("user_id"));
    console.log("relative_user_id", cookie.load("relative_user_id"));
    console.log("check_user_id", cookie.load("check_user_id"));
    cookie.save("userId", "userId", { path: "/" });

    if (typeof is_contest === "undefined") {
    } else if (is_contest === "true") {
      let redirect = this.props.history;
      let contest_id = cookie.load("refered_contest_id");
      console.log(redirect);
      // redirect.push("/Contest/" + contest_id + "/" + userid);
    }

    this.state = {
      email: "",
      open: false,
      alertmsg: "",
      is_login: false,
      login: "",
      loader: false,
      nocontest: false,
      push_contest: false,
      show_error: false,
      error: ""
    };
    this.handleSignup.bind(this);
    this.handleLogin.bind(this);
  }

  componentDidMount = () => {
    path = window.location;
    console.log("path", path);
    localStorage.removeItem("login_failed_email");
    localStorage.setItem("pathname", window.location.pathname);
    console.log("pathname", localStorage.getItem("pathname"));

    // To handle auth0 login and singup link
    if (path.hash.length > 0) {
      console.log("login hash ");
      let cname = [];
      let tempname = path.hash.trim();
      cname = tempname.split("&");
      this.setState({ loader: true });
      //access token
      let token = [];
      let temp_token = cname[0].trim();
      token = temp_token.split("=");
      console.log("cname hash", cname);
      console.log("token hash", token);

      //link expires
      if (token[1] === "unauthorized") {
        this.setState({ loader: false });
        this.setState({
          show_error: true,
          error: "Link expires..."
        });
      }
      //signup_link
      else if (token[0] === "#signup") {
        localStorage.setItem("current_state", "signup"),
          this.props.history.push("/signup_step-1");
      }
      //login_link
      else if (token[0] === "#login") {
        localStorage.setItem("current_state", "login"),
          this.props.history.push("/login");
      }
      // Unsubscribe link
      else if (token[0] === "#unsubscribe?to") {
        let reciever = base64.decode(token[1]);
        console.log("reciever hash", reciever);
        let token1 = [];
        let temp_token1 = cname[1].trim();
        token1 = temp_token1.split("=");
        console.log("token1", token1);
        let sender = base64.decode(token1[1]);
        console.log("sender", sender);
        localStorage.setItem("unsubscribe_reciever", reciever);
        localStorage.setItem("unsubscribe_sender", sender);
        localStorage.setItem("current_state", "unsubscribe"),
          this.props.history.push("/unsubscribe");
      }
      //Winner link
      else if (token[1] === "winner_prize") {
        let self = this;
        console.log("here winner");
        console.log("cname winner", cname);
        let contest_id = [];
        let temp_contest_id = cname[1].trim();
        contest_id = temp_contest_id.split("=");
        console.log("contest_id", contest_id[1]);
        localStorage.setItem("winner_contest_id", contest_id[1]);
        cookie.save("winner_contest_id", contest_id[1], { path: "/" });
        let encrypt_user_id = [];
        let temp_user_id = cname[2].trim();
        encrypt_user_id = temp_user_id.split("=");
        let user_id = base64.decode(encrypt_user_id[1]);
        console.log("user_id", user_id);
        // localStorage.setItem("user_id",user_id)
        let claim_for = [];
        let temp_claim_for = cname[3].trim();
        claim_for = temp_claim_for.split("=");
        let claim_for_id = claim_for[1];
        console.log("claim_for_id", claim_for_id);
        localStorage.setItem("claim_for", claim_for_id);
        cookie.save("claim_for", claim_for_id, { path: "/" });
        console.log("user_id", cookie.load("user_id"));
        console.log("user_id", decodeURI(user_id));
        if (cookie.load("user_id") === decodeURI(user_id)) {
          let get_contest = {
            action: "getcontest",
            user_id: user_id,
            contest_id: contest_id[1]
          };
          console.log("get_contest", get_contest);
          axios
            .request({
              method: "POST",
              url: api.react_request,
              data: JSON.stringify(get_contest)
            })
            .then(function(get_contest_response) {
              console.log("response", get_contest_response);
              if (get_contest_response.data) {
                localStorage.setItem(
                  "is_contest",
                  get_contest_response.data[0].is_contest
                );
                localStorage.setItem(
                  "contest_array",
                  JSON.stringify(get_contest_response.data)
                );
                localStorage.setItem(
                  "need_to_claim",
                  JSON.stringify(get_contest_response.data)
                );

                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(get_contest_response.data[0])
                  );
                  localStorage.setItem(
                    "product_id",
                    JSON.stringify(get_contest_response.data[0].product_id)
                  );
                  cookie.save("winner_contest_id",JSON.parse(localStorage.getItem("need_to_claim_detail")).contest_id,{path:'/'})
                  cookie.save("claim_for",JSON.parse(localStorage.getItem("need_to_claim_detail")).id,{path:'/'})
                  
                  self.props.history.push("/won");
                }

                localStorage.setItem(
                  "contest_detail",
                  JSON.stringify(get_contest_response.data[0])
                );
                localStorage.setItem("contest_index", 0);
                if (get_contest_response.data[0]._id === "15") {
                  console.log("")
                  if (localStorage.getItem("product_id") === "2"|| localStorage.getItem("product_id") === 2) {
                    localStorage.setItem(
                      "current_state",
                      "product_orientation3"
                    );
                    self.props.history.push("/product_orientation3");
                  } else {
                    localStorage.setItem(
                      "current_state",
                      "product_orientation4"
                    );
                    self.props.history.push("/product_orientation4");
                  }
                } else {
                  localStorage.setItem("current_state", "won1"),
                    self.props.history.push("/won1");
                }
              }
            })
            .catch(error => {
              console.log("error", error);
              self.setState({ loader: false });
              self.setState({
                show_error: true,
                error: "Something Wrong... Please try Again"
              });
            });
        } else {
          console.log("here");
          self.setState({ loader: false });
          self.props.history.push("/");
        }
      }

      //Past member link
      else if (token[0] === "#past_member/user_id") {
        let self = this;
        localStorage.setItem("check_user_id", token[1]);
        cookie.save("check_user_id", token[1], { path: "/" });
        localStorage.setItem("current_state", "past_member_orientation");
        self.props.history.push("/past_member_orientation");
      }
      //connect link manage #Connect
      else if (token[0] === "#Connect/user_id") {
        let self = this;
        cookie.save("relative_refered_by_user_id", token[2], { path: "/" });
        let id = [];
        let temp_id = token[1].trim();
        id = temp_id.split("/");
        cookie.save("relative_user_id", id[0], { path: "/" });
        console.log(
          "relative_refered_by_user_id",
          cookie.load("relative_refered_by_user_id")
        );
        console.log("relative_user_id", cookie.load("relative_user_id"));
        // localStorage.setItem("current_state", "connect");
        // self.setState({ loader: false });
        // self.props.history.push(
        //   "/Connect/" +
        //     cookie.load("relative_user_id") +
        //     "/" +
        //     cookie.load("relative_refered_by_user_id")
        // );
        localStorage.setItem(
          "current_state",
          "new_member_orientation"
        );
        self.setState({ loader: false });
        self.props.history.push("/new_member_orientation");
      }
      //login process through auth0
      else {
        localStorage.setItem("access_token", token[1]);
        cookie.save("access_token", token[1], { path: "/" });
        console.log("access_token", cookie.load("access_token"));

        //Get user_id
        let id = [];
        let temp_id = cname[1].trim();
        id = temp_id.split("=");
        var linked_user_info = jwt_decode(id[1]);
        console.log("linked_user_info", linked_user_info);

        //get user info
        let self = this;
        let get_user_information = {
          action: "get_information_nd_contest",
          user_id: linked_user_info.sub,
          get_contest: {
            user_id: linked_user_info.sub,
            is_active: 1
          }
        };
        console.log("get_user_information", get_user_information);

        axios
          .request({
            method: "POST",
            url: api.react_request,
            data: JSON.stringify(get_user_information)
          })
          .then(function(login_response) {
            console.log("login_response", login_response);
            if (login_response.status === 200) {
              // Login successfull

              console.log("login_response successfull");
              localStorage.setItem("login_is_active", 1);
              cookie.save(
                "user_id",
                login_response.data.user_information.user_id,
                { path: "/" }
              );
              localStorage.setItem(
                "user_id",
                login_response.data.user_information.user_id
              );
              localStorage.setItem(
                "phone_number",
                login_response.data.user_information.user_metadata.phone_number
              );
              localStorage.setItem(
                "child_data",
                JSON.stringify(
                  login_response.data.user_information.user_metadata.child_data
                )
              );
              localStorage.setItem(
                "address",
                JSON.stringify(
                  login_response.data.user_information.user_metadata.address
                )
              );
              if (login_response.data.user_information.user_metadata.address) {
                localStorage.setItem(
                  "addressLine1",
                  login_response.data.user_information.user_metadata.address
                    .addressLine1
                );
                localStorage.setItem(
                  "addressLine2",
                  login_response.data.user_information.user_metadata.address
                    .addressLine2
                );
                localStorage.setItem(
                  "city",
                  login_response.data.user_information.user_metadata.address
                    .city
                );
                localStorage.setItem(
                  "state",
                  login_response.data.user_information.user_metadata.address
                    .state
                );
                localStorage.setItem(
                  "zipcode",
                  login_response.data.user_information.user_metadata.address
                    .zipcode
                );
              }
              localStorage.setItem(
                "related_entrant_orientation",
                login_response.data.user_information.user_metadata
                  .related_entrant_orientation
              );
              localStorage.setItem(
                "someone_else_orientation",
                login_response.data.user_information.user_metadata
                  .someone_else_orientation
              );
              localStorage.setItem(
                "skill_test_question",
                login_response.data.user_information.user_metadata
                  .skillTestQuestion
              );
              localStorage.setItem(
                "user_age_required",
                login_response.data.user_information.user_metadata
                  .user_age_required
              );
              localStorage.setItem("is_signup", "false");
              localStorage.setItem(
                "relation_data",
                JSON.stringify(
                  login_response.data.user_information.user_metadata.relation
                )
              );
              localStorage.setItem(
                "perks",
                login_response.data.user_information.user_metadata.perks
              );
              localStorage.setItem(
                "offer_orientation",
                login_response.data.user_information.user_metadata
                  .offer_orientation
              );
              localStorage.setItem(
                "contest_orientation",
                login_response.data.user_information.user_metadata
                  .contest_orientation
              );

              localStorage.setItem(
                "first_name",
                login_response.data.user_information.user_metadata.first_name
              );
              localStorage.setItem(
                "last_name",
                login_response.data.user_information.user_metadata.last_name
              );
              localStorage.setItem(
                "user_email",
                login_response.data.user_information.email
              );
              localStorage.setItem(
                "need_to_claim",
                JSON.stringify(login_response.data.need_to_claim)
              );
              
              if (login_response.data.need_to_claim) {
                localStorage.setItem(
                  "need_to_claim_detail",
                  JSON.stringify(login_response.data.need_to_claim[0])
                );
                localStorage.setItem(
                  "product_id",
                  JSON.stringify(login_response.data.need_to_claim[0].product_id)
                );
                cookie.save("winner_contest_id",JSON.parse(localStorage.getItem("need_to_claim_detail")).contest_id,{path:'/'})
                cookie.save("claim_for",JSON.parse(localStorage.getItem("need_to_claim_detail")).id,{path:'/'})
              }


              console.log(
                "login_response.data.user_metadata.link_activated",
                login_response.data.user_information.user_metadata
                  .link_activated
              );

              if (
                login_response.data.user_information.user_metadata
                  .link_activated === 1
              ) {
                if (
                  login_response.data.user_information.user_metadata.address
                ) {
                  console.log("child_data", localStorage.getItem("child_data"));

                  localStorage.setItem(
                    "child_data",
                    JSON.stringify(
                      login_response.data.user_information.user_metadata
                        .child_data
                    )
                  );

                  if (
                    typeof cookie.load("relative_user_id") !== undefined &&
                    cookie.load("relative_user_id") !== "undefined" &&
                    cookie.load("relative_user_id") !== "null" &&
                    cookie.load("relative_user_id") !== null &&
                    cookie.load("relative_user_id")
                  ) {
                    console.log(
                      "contest_id relative_user_id",
                      cookie.load("relative_user_id")
                    );
                    cookie.save(
                      "relative_user_id",
                      login_response.data.user_information.user_id,
                      { path: "/" }
                    );
                    localStorage.setItem(
                      "current_state",
                      "new_member_orientation"
                    );
                    self.setState({ loader: false });
                    self.props.history.push("/new_member_orientation");
                    // localStorage.setItem("current_state", "connect");
                    // self.setState({ loader: false });
                    // self.props.history.push(
                    //   "/Connect/" +
                    //     cookie.load("relative_user_id") +
                    //     "/" +
                    //     cookie.load("relative_refered_by_user_id")
                    // );
                  } else if (
                    typeof cookie.load("refered_contest_id") !== undefined &&
                    cookie.load("refered_contest_id") !== "undefined" &&
                    cookie.load("refered_contest_id") !== "null" &&
                    cookie.load("refered_contest_id") !== null &&
                    cookie.load("refered_contest_id")
                  ) {
                    console.log(
                      "contest_id refered",
                      cookie.load("refered_contest_id")
                    );
                    login_response.data.contest_response.forEach(
                      (element, index) => {
                        if (
                          element.contest_id ===
                          cookie.load("refered_contest_id")
                        ) {
                          localStorage.setItem(
                            "is_contest",
                            element.is_contest
                          );
                          console.log("index", index);
                          localStorage.setItem(
                            "contest_array",
                            JSON.stringify(login_response.data.contest_response)
                          );
                          localStorage.setItem(
                            "contest_detail",
                            JSON.stringify(element)
                          );
                          localStorage.setItem("contest_index", index);
                        }
                      }
                    );
                    localStorage.setItem("current_state", "contest");
                    self.setState({ loader: false });
                    self.props.history.push(
                      "/Contest/" +
                        cookie.load("refered_contest_id") +
                        "/" +
                        cookie.load("user_id")
                    );
                  }
                  //winning contest link
                  else if (
                    typeof cookie.load("winner_contest_id") !== undefined &&
                    cookie.load("winner_contest_id") !== "undefined" &&
                    cookie.load("winner_contest_id") !== "null" &&
                    cookie.load("winner_contest_id") !== null &&
                    cookie.load("winner_contest_id") &&
                    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 &&
                    localStorage.getItem("need_to_claim")
                  ) {
                    console.log(
                      "winner_contest_id",
                      cookie.load("winner_contest_id")
                    );
                    let get_contest = {
                      action: "getcontest",
                      user_id: cookie.load("user_id"),
                      contest_id: cookie.load("winner_contest_id"),
                      is_active: 1
                    };
                    console.log("get_contest", get_contest);
                    axios
                      .request({
                        method: "POST",
                        url: api.react_request,
                        data: JSON.stringify(get_contest)
                      })
                      .then(function(get_contest_response) {
                        console.log("response", get_contest_response);
                        if (get_contest_response.data) {
                          localStorage.setItem(
                            "is_contest",
                            get_contest_response.data[0].is_contest
                          );
                          localStorage.setItem(
                            "contest_array",
                            JSON.stringify(get_contest_response.data)
                          );
                          localStorage.setItem(
                            "contest_detail",
                            JSON.stringify(get_contest_response.data[0])
                          );
                          localStorage.setItem("contest_index", 0);
                          if (get_contest_response.data[0]._id === "15") {
                            if (localStorage.getItem("product_id") === "2"||localStorage.getItem("product_id") === 2) {
                              localStorage.setItem(
                                "current_state",
                                "product_orientation3"
                              );
                              self.props.history.push("/product_orientation3");
                            } else {
                              localStorage.setItem(
                                "current_state",
                                "product_orientation4"
                              );
                              self.props.history.push("/product_orientation4");
                            }
                          } else {
                            localStorage.setItem("current_state", "won1"),
                              self.props.history.push("/won1");
                          }
                        } else {
                          self.setState({
                            open: true,
                            alertmsg: "This Contest Is Not Live"
                          });
                        }
                      })
                      .catch(error => {
                        console.log("error", error);
                        self.setState({ loader: false });
                        self.setState({
                          show_error: true,
                          error: "Something Wrong... Please try Again"
                        });
                      });
                  } else {
                    // console.log(
                    //   "contest_response",
                    //   login_response.data.contest_response
                    // );

                    self.setState({ loader: false });
                    let contest = [];
                    let freebies = [];
                    localStorage.setItem("current_state", "contest");
                    if (login_response.data.contest_response.length > 0) {
                      login_response.data.contest_response.forEach(element => {
                        console.log("element", element);
                        if (element.direct == 0 || element.direct == "0") {
                          localStorage.setItem(
                            "current_state",
                            "related_entrant_orientation"
                          );
                          self.props.history.push(
                            "/related_entrant_orientation"
                          );
                        } else {
                          self.setState({ push_contest: true });
                        }
                        if (element.is_contest == 0) {
                          freebies.push(element);
                          self.setState({
                            is_freebies: true,
                            freebies: freebies
                          });
                        } else {
                          contest.push(element);
                          self.setState({ is_contest: true, contest: contest });
                        }
                      });
                      if (self.state.push_contest) {
                        localStorage.setItem(
                          "is_contest",
                          login_response.data.contest_response[0].is_contest
                        );
                        localStorage.setItem(
                          "contest_array",
                          JSON.stringify(login_response.data.contest_response)
                        );
                        localStorage.setItem(
                          "contest_detail",
                          JSON.stringify(
                            login_response.data.contest_response[0]
                          )
                        );
                        localStorage.setItem("contest_index", 0);
                        console.log(
                          JSON.parse(localStorage.getItem("contest_array"))
                        );
                        localStorage.setItem("current_state", "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]
                              )
                            );
                            localStorage.setItem(
                              "product_id",
                              JSON.stringify(login_response.data.need_to_claim[0].product_id)
                            );
                            cookie.save("winner_contest_id",JSON.parse(localStorage.getItem("need_to_claim_detail")).contest_id,{path:'/'})
                            cookie.save("claim_for",JSON.parse(localStorage.getItem("need_to_claim_detail")).id,{path:'/'})
                          self.props.history.push("/won");
                        } else {
                          if (freebies.length > 0) {
                            localStorage.setItem("current_state", "contest"),
                              self.props.history.push(
                                "/Contest/" +
                                  login_response.data.contest_response[0]._id +
                                  "/" +
                                  cookie.load("user_id")
                              );
                          } else {
                            localStorage.setItem("current_state", "no_freebie"),
                              self.props.history.push("/no_freebie");
                          }
                        }
                      }
                    } 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", "full_mailing_address");
                  self.props.history.push("/signup_step-2");
                }
              } else {
                console.log(
                  "login_response.data.link_activated",
                  login_response.data.user_information.user_metadata
                    .link_activated
                );
                localStorage.setItem("login_is_active", 1);
                localStorage.setItem(
                  "user_id",
                  login_response.data.user_information.user_id
                );
                localStorage.setItem("current_state", "account_activated"),
                  self.props.history.push("/account_activated");
              }
            } else {
              console.log("error");
              self.setState({ loader: false });
              self.setState({
                nocontest: false,
                show_error: true,
                error: "Something wrong... Please try again later"
              });
            }
          })
          .catch(error => {
            console.log("error", error);
            self.setState({ loader: false });
            self.setState({
              nocontest: false,
              show_error: true,
              error: "Something wrong... Please try again later"
            });
          });
      }
    }
    //To handle family shared contest link
    else if (path.search !== "") {
      let self = this;
      console.log("here");
      // If user is already logged in
      if (
        typeof cookie.load("user_id") !== undefined &&
        cookie.load("user_id") !== "undefined" &&
        cookie.load("user_id") !== "null" &&
        cookie.load("user_id") !== null &&
        cookie.load("user_id")
      ) {
        if (cookie.load("user_id") === cookie.load("check_user_id")) {
          console.log("login");
          localStorage.getItem("is_signup", "false");
          localStorage.setItem("login_is_active", 1);
          console.log("contest_id refered", cookie.load("refered_contest_id"));
          localStorage.setItem("current_state", "contest");
          self.setState({ loader: false });
          self.props.history.push(
            "/Contest/" +
              cookie.load("refered_contest_id") +
              "/" +
              cookie.load("user_id")
          );
        } else {
          console.log("not logged in");

          //family connect
        }
      }

      // If user is not logged in
      else {
        console.log("not logged in");

        //family connect
      }
    } else if (
      typeof cookie.load("relative_user_id") !== undefined &&
      cookie.load("relative_user_id") !== "undefined" &&
      cookie.load("relative_user_id") !== "null" &&
      cookie.load("relative_user_id") !== null &&
      cookie.load("relative_user_id")
    ) {
      if (cookie.load("relative_user_id") === cookie.load("user_id")) {
        console.log(
          "contest_id relative_user_id",
          cookie.load("relative_user_id")
        );
        let self = this;
        self.setState({ loader: true });
        let get_user_information = {
          action: "get_information_nd_contest",
          user_id: cookie.load("relative_user_id"),
          get_contest: {
            user_id: cookie.load("relative_user_id"),
            is_active: 1
          }
        };
        console.log("get_user_information", get_user_information);

        axios
          .request({
            method: "POST",
            url: api.react_request,
            data: JSON.stringify(get_user_information)
          })
          .then(function(response) {
            console.log("response", response);
            if (response.status === 200) {
              if (response.data.user_information.user_metadata.relation) {
                localStorage.setItem(
                  "relation_data",
                  JSON.stringify(
                    response.data.user_information.user_metadata.relation
                  )
                );
              }
              if (
                response.data.user_information.user_metadata.is_active !== 1
              ) {
                localStorage.setItem("current_state", "new_member_orientation");
                self.setState({ loader: false });
                self.props.history.push("/new_member_orientation");
              } else {
                self.setState({ loader: false });
                console.log(
                  "response.data.user_metadata.is_active",
                  response.data.user_information.user_metadata.is_active
                );
                console.log(
                  "contest_id relative_user_id",
                  cookie.load("relative_user_id")
                );
                // cookie.save(
                //   "relative_user_id",
                //   login_response.data.user_id,
                //   { path: "/" }
                // );

                localStorage.setItem("current_state", "connect");
                self.setState({ loader: false });
                self.props.history.push(
                  "/Connect/" +
                    cookie.load("relative_user_id") +
                    "/" +
                    cookie.load("relative_refered_by_user_id")
                );
              }
            }
          })
          .catch(error => {
            console.log("error", error);
            self.setState({ loader: false });
            self.setState({
              show_error: true,
              error: "Something Wrong... Please try Again"
            });
          });
      }
    } else {
      let self = this;
      //yahan condition lagani hai session wali
      if (
        typeof cookie.load("access_token") !== undefined &&
        cookie.load("access_token") !== "undefined" &&
        cookie.load("access_token") !== "null" &&
        cookie.load("access_token") !== null &&
        cookie.load("access_token")
      ) {
        localStorage.setItem("current_state", "menu_page");
        self.setState({ loader: false });
        self.props.history.push("/menu_page");
      } else {
        console.log("not logged in");
        console.log("path.hash.length", path.hash.length);
      }
    }
  };

  handleLogin = () => {
    let redirect = this.props.history;
    this.setState({ is_login: true });
    cookie.remove("access_token", { path: "/" });
    localStorage.removeItem("child_data");
    localStorage.removeItem("phone_number");
    localStorage.removeItem("relation_data");
    localStorage.removeItem("contest_array");
    localStorage.removeItem("contest_detail");
    localStorage.removeItem("contest_id");
    localStorage.removeItem("contest_index");
    localStorage.removeItem("skill_test_question");
    localStorage.removeItem("perks");
    localStorage.removeItem("user_birthdate");
    localStorage.removeItem("user_age_required");
    localStorage.removeItem("upgraded");
    localStorage.removeItem("total_price");
    localStorage.removeItem("someone_else_orientation");
    localStorage.removeItem("shipping_price");
    localStorage.removeItem("relation_with_children1");
    localStorage.removeItem("relation_with_children");
    localStorage.removeItem("related_last_name");
    localStorage.removeItem("related_first_name");
    localStorage.removeItem("related_entrant_orientation");
    localStorage.removeItem("product_price");
    localStorage.removeItem("product_name");
    localStorage.removeItem("product_id");
    localStorage.removeItem("contest_title");
    localStorage.removeItem("is_edit");
    cookie.remove("user_id", { path: "/" });
    localStorage.removeItem("need_to_claim_detail");
    localStorage.removeItem("need_to_claim");
    redirect.push("/login");
  };

  handleSignup = () => {
    let redirect = this.props.history;
    this.setState({ is_login: false });
    cookie.remove("access_token", { path: "/" });
    localStorage.removeItem("phone_number");
    localStorage.setItem("current_state", "signup");
    localStorage.removeItem("login_is_active");
    localStorage.removeItem("child_data");
    localStorage.removeItem("relation_data");
    localStorage.removeItem("contest_array");
    localStorage.removeItem("contest_detail");
    localStorage.removeItem("contest_id");
    localStorage.removeItem("contest_index");
    localStorage.removeItem("skill_test_question");
    localStorage.removeItem("perks");
    localStorage.removeItem("user_birthdate");
    localStorage.removeItem("user_age_required");
    localStorage.removeItem("upgraded");
    localStorage.removeItem("total_price");
    localStorage.removeItem("someone_else_orientation");
    localStorage.removeItem("shipping_price");
    localStorage.removeItem("relation_with_children1");
    localStorage.removeItem("relation_with_children");
    localStorage.removeItem("related_last_name");
    localStorage.removeItem("related_first_name");
    localStorage.removeItem("related_entrant_orientation");
    localStorage.removeItem("product_price");
    localStorage.removeItem("product_name");
    localStorage.removeItem("product_id");
    localStorage.removeItem("contest_title");
    localStorage.removeItem("is_edit");
    localStorage.removeItem("user_id");
    cookie.remove("user_id", { path: "/" });
    localStorage.removeItem("need_to_claim_detail");
    localStorage.removeItem("need_to_claim");
    redirect.push("/signup_step-1");
  };

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

  render() {
    return (
      <center>
        <div className="background-images">
          <div className="background_color">
            <StyleRoot>
              <div className="home_btn home_width" style={styles.slideInRight}>
                <div className="container-fluid">
                  <div style={styles.root}>
                    {this.state.loader && (
                      <center>
                        <CircularProgress
                          size={100}
                          style={{ color: "black", marginTop: "17%" }}
                        />
                      </center>
                    )}
                    <Dialog
                      style={{ textTransform: "capitalize" }}
                      actions={
                        <FlatButton
                          label="Close"
                          onClick={() =>
                            this.state.nocontest
                              ? (localStorage.setItem(
                                  "current_state",
                                  "set_relation"
                                ),
                                this.props.history.push("/set_relation"))
                              : this.handleClose()
                          }
                        />
                      }
                      modal={false}
                      open={this.state.open}
                    >
                      {this.state.alertmsg}
                    </Dialog>
                    <div>
                      {!this.state.is_login &&
                        !this.state.loader && (
                          <div className="allpages_desktop_boxes_div">
                            <StyleRoot>
                              <div style={styles.slideInRight}>
                                <div className="row">
                                  <p className="home_para homepage_para">
                                    WELCOME TO
                                  </p>
                                </div>
                                <div className="row">
                                  <img
                                    src={api.image_path + "familyone_logo.png"}
                                    height="80px"
                                  />
                                </div>
                                <div className="row">
                                  <p className="home_para homepage_para homepage_para1">
                                    A Members Only
                                    <br />
                                    Community for Families
                                  </p>
                                  <p className="home_para homepage_para2">
                                    Dedicated to providing families with
                                    freebies.
                                    <br />
                                    contests, special offers, packages, and
                                    more..
                                  </p>
                                </div>
                                <div className="home_btn">
                                  <button
                                    className="btn_signup"
                                    onClick={this.handleSignup}
                                  >
                                    Sign up
                                  </button>
                                  <button
                                    className="btn_login"
                                    onClick={this.handleLogin}
                                  >
                                    Log in
                                  </button>
                                </div>
                                {this.state.show_error && (
                                  <div className="bcd_shownotification_div">
                                    <img
                                      src={
                                        api.image_path + "error-msg-icon.png"
                                      }
                                      alt="notify img"
                                    />
                                    <p className="notify_para">
                                      {this.state.error}
                                    </p>
                                    <div className="bcd_shownotification_div1">
                                      <a
                                        onClick={() => {
                                          this.setState({ show_error: false });
                                        }}
                                        className="notify_para1"
                                      >
                                        X
                                      </a>
                                    </div>
                                  </div>
                                )}
                              </div>
                            </StyleRoot>
                          </div>
                        )}

                      {this.state.is_login && (
                        <div className="row">
                          <div className="col-md-6 col-lg-6 ">
                            <img
                              src={api.image_path + "prev_arrow_one.jpg"}
                              width="40"
                              className="next_icon next_left"
                              onClick={() => {
                                this.setState({
                                  is_login: false,
                                  alertmsg: ""
                                });
                              }}
                            />
                          </div>
                        </div>
                      )}
                    </div>
                  </div>
                </div>
              </div>
            </StyleRoot>
          </div>
        </div>
      </center>
    );
  }
}

const styles = {
  button: { margin: 12 },
  slideInRight: {
    animation: "x 1s",
    animationName: Radium.keyframes(slideInRight, "slideInRight")
  }
};

export default Home;
