// On create view method called due the Fragment API being extended
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout hazardperception as a view, which can then be manipulated and
    // Returned (due to View nature of Function)
    View v = inflater.inflate(R.layout.hazardperception, container, false);

    // If the screen contains views, clear them
    if (mainLayout != null) mainLayout.removeAllViewsInLayout();

    // Import from arrays.xml / declaring objects to be used
    listOfItems = new ArrayList<HazardPerceptionInstance>();
    videoIDs = getResources().getStringArray(R.array.hazardperception);
    mainLayout = (ListView) v.findViewById(R.id.hazardperception_list);

    // Importing scores from memory
    String scores =
        getActivity()
            .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
            .getString("HAZARDPERCEPTIONSCORE", "Error");

    // Error checking the "scores" String
    if (scores.equals("Error")) {
      createBlankScores("HAZARDPERCEPTIONSCORE", "0");
      scoresSplitter =
          getActivity()
              .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
              .getString("HAZARDPERCEPTIONSCORE", "Error")
              .split("#");
    } else if (getActivity()
            .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
            .getString("HAZARDPERCEPTIONSCORE", "Error")
            .split("#")
            .length
        != getResources().getStringArray(R.array.hazardperception).length) {
      createBlankScores("HAZARDPERCEPTIONSCORE", "0");
      scoresSplitter =
          getActivity()
              .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
              .getString("HAZARDPERCEPTIONSCORE", "Error")
              .split("#");

    } else {
      scoresSplitter = scores.split("#");
    }

    // Importing which video's are "viewed" from memory
    String viewed =
        getActivity()
            .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
            .getString("HAZARDPERCEPTION", "Error");

    // Error checking the "viewed" String
    if (viewed.equals("Error")) {
      createBlankScores("HAZARDPERCEPTION", "false");
      viewedSplitter =
          getActivity()
              .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
              .getString("HAZARDPERCEPTION", "Error")
              .split("#");
    } else if (getActivity()
            .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
            .getString("HAZARDPERCEPTION", "Error")
            .split("#")
            .length
        != getResources().getStringArray(R.array.hazardperception).length) {
      createBlankScores("HAZARDPERCEPTION", "false");
      viewedSplitter =
          getActivity()
              .getSharedPreferences(MainActivity.spName, Activity.MODE_PRIVATE)
              .getString("HAZARDPERCEPTION", "Error")
              .split("#");

    } else {
      viewedSplitter = viewed.split("#");
    }

    // Adding an item to the List for every hazard perception video
    for (int i = 0; i < videoIDs.length; i++) {
      listOfItems.add(
          new HazardPerceptionInstance(
              i + 1, Integer.parseInt(scoresSplitter[i]), viewedSplitter[i]));
    }

    // Initialise the adapter in accordance with the list of hazardperception videos, and the layout
    // from which it's based
    hazardAdapter =
        new HazardPerceptionAdapter(getActivity(), R.layout.hazardperceptionview, listOfItems);

    // Setting the layout adapter
    mainLayout.setAdapter(hazardAdapter);
    mainLayout.setDivider(null);

    // Launching the "HazardPerceptionElement" class when an item is clicked.
    mainLayout.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Intent i = new Intent("ACTIVITY_HAZARDPERCEPTIONELEMENT");
            i.putExtra(
                "POSITION",
                position); // Will transfer through "position" to "HazardPerceptionElement"
            i.putExtra("REVIEW", false);
            startActivity(i);
          }
        });

    mainLayout.setOnItemLongClickListener(
        new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(
              AdapterView<?> adapterView, View view, int position, long l) {
            Intent i = new Intent("ACTIVITY_HAZARDPERCEPTIONELEMENT");
            i.putExtra(
                "POSITION",
                position); // Will transfer through "position" to "HazardPerceptionElement"
            i.putExtra("REVIEW", true);
            startActivity(i);
            return true;
          }
        });

    if (!getActivity()
        .getSharedPreferences(MainActivity.spName, Context.MODE_PRIVATE)
        .getBoolean("HAZARD_SEEN", false)) {
      ShowcaseView.ConfigOptions options = new ShowcaseView.ConfigOptions();
      if (MainActivity.showOnce) options.shotType = ShowcaseView.TYPE_ONE_SHOT;
      options.showcaseId = 6;
      ShowcaseView.insertShowcaseView(
          mainLayout,
          getActivity(),
          R.string.tutorial_06_title,
          R.string.tutorial_06_message,
          options);
      getActivity()
          .getSharedPreferences(MainActivity.spName, Context.MODE_PRIVATE)
          .edit()
          .putBoolean("HAZARD_SEEN", true)
          .commit();
    }
    return v;
  }
    // shows an overlay tutorial based on the number you give it in the order, and all after
    private void showTutorial(final int tutorialNumber) {
      View.OnClickListener clickListener =
          new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              showTutorial(tutorialNumber + 1);
            }
          };
      ViewTarget target;
      ActionViewTarget target1;
      String title;
      String message = "";
      switch (tutorialNumber) {
        case 1:
          target = new ViewTarget(getActivity().findViewById(R.id.getbutton));
          title = "First, install and run Foldit on your computer.";
          message = "Foldit on Android doesn't work without the game running on your computer!";
          break;
        case 2:
          getActivity().findViewById(R.id.social).setVisibility(View.VISIBLE);
          target = new ViewTarget(getActivity().findViewById(R.id.social));
          title = "On your computer, open a puzzle in Foldit. Next, navigate open the social tab.";
          message = "Once you have done this, click next.";
          break;
        case 3:
          getActivity().findViewById(R.id.social).setVisibility(View.INVISIBLE);
          getActivity().findViewById(R.id.remote).setVisibility(View.VISIBLE);
          target = new ViewTarget(getActivity().findViewById(R.id.social));
          title = "Click on remote connection, then click 'Turn on'.";
          message = "Note your local IP address as shown in the pop-up box.";
          break;
        case 4:
          getActivity().findViewById(R.id.remote).setVisibility(View.INVISIBLE);
          target = new ViewTarget(getActivity().findViewById(R.id.editAddress));
          title = "Copy the IP address of your computer here.";
          message = "Foldit will show your IP address in the Remote Connection popup.";
          break;
        case 5:
          target = new ViewTarget(getActivity().findViewById(R.id.playbutton));
          title = "Hit Connect and begin your protein folding adventure!";
          message = "A Wi-Fi connection is recommended.";
          break;
        case 6:
          target1 = new ActionViewTarget(getActivity(), ActionViewTarget.Type.OVERFLOW);
          title = "Visit the settings to change connection quality.";
          message = "This can make a big difference in performance!";
          if (sv != null) {
            sv.setText(title, message);
            sv.setShowcase(target1, true);
          }
          sv.overrideButtonClick(clickListener);
          return;
        case 7:
          target1 = new ActionViewTarget(getActivity(), ActionViewTarget.Type.HOME);
          title = "For more information, visit the navigation drawer.";
          message = "Help, About, and Wiki!";
          if (sv != null) {
            clickListener =
                new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                    ((ShowcaseView) view.getParent()).hide();
                  }
                };
            sv.setText(title, message);
            sv.setShowcase(target1, true);
            sv.overrideButtonClick(clickListener);
            sv = null;
            SharedPreferences myPrefs = getActivity().getSharedPreferences("myPrefs", MODE_PRIVATE);
            if (!myPrefs.contains("tutorialFinished")) {
              myPrefs
                  .edit()
                  .putString("tutorialFinished", "true")
                  .commit(); // Open the drawer on first install of app only
            }
          }
        default:
          return; // don't show a message
      }

      if (sv == null) {
        ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions();
        co.hideOnClickOutside = true;
        co.shotType = ShowcaseView.TYPE_ONE_SHOT;
        sv = ShowcaseView.insertShowcaseView(target, getActivity(), title, message, co);
      } else {
        sv.setText(title, message);
        sv.setShowcase(target, true);
      }
      sv.overrideButtonClick(clickListener);
    }