/**
     *  Implements the form() function as required by WordPress
     *  This is responsible for how the form in the WordPress admin looks
     */
    function form($instance)
    {
        $defaults = array('title' => '', 'button_text' => 'Checkout with Instamojo', 'instamojo_offer' => '', 'button_style' => 'none');
        $instance = wp_parse_args((array) $instance, $defaults);
        $instamojo_credentials = get_option('instamojo_credentials');
        if (!$instamojo_credentials['auth_token']) {
            ?>
      <p>Please authenticate your account first.</p>
      <?php 
        } else {
            // Create Instamojo instance for interacting with API
            $instamojo = new Instamojo(APPLICATION_ID);
            $instamojo->setAuthToken($instamojo_credentials['auth_token']);
            $offerObject = $instamojo->listAllOffers();
            $offers = $offerObject['offers'];
            ?>
      <p>
        <label for="<?php 
            echo $this->get_field_id('title');
            ?>
">Widget Title:</label>
        <input class="widefat" id="<?php 
            echo $this->get_field_id('title');
            ?>
"
          name="<?php 
            echo $this->get_field_name('title');
            ?>
"
          value="<?php 
            echo $instance['title'];
            ?>
" />
      </p>
      <p>
        <label for="<?php 
            echo $this->get_field_id('button_text');
            ?>
">Button Text:</label>
        <input class="widefat" id="<?php 
            echo $this->get_field_id('button_text');
            ?>
"
          name="<?php 
            echo $this->get_field_name('button_text');
            ?>
"
          value="<?php 
            echo $instance['button_text'];
            ?>
" />
      </p>
      <p>
        <label for="<?php 
            echo $this->get_field_id('instamojo_offer');
            ?>
">Instamojo Offer:</label>
        <select id="<?php 
            echo $this->get_field_id('instamojo_offer');
            ?>
" name="<?php 
            echo $this->get_field_name('instamojo_offer');
            ?>
">
          <option value="none" <?php 
            if ($instance['instamojo_offer'] == '') {
                echo 'selected="selected"';
            }
            ?>
>None</option>
        <?php 
            foreach ($offers as $offer) {
                ?>
          <option value="<?php 
                echo $offer['slug'];
                ?>
" <?php 
                if ($instance['instamojo_offer'] == $offer['slug']) {
                    echo 'selected="selected"';
                }
                ?>
><?php 
                echo $offer['title'];
                ?>
</option>
        <?php 
            }
            ?>
        </select>
      </p>
      <p>
        <label for="<?php 
            echo $this->get_field_id('button_style');
            ?>
">Button Style</label>
        <select id="<?php 
            echo $this->get_field_id('button_style');
            ?>
" name="<?php 
            echo $this->get_field_name('button_style');
            ?>
">
          <option value="light" <?php 
            if ($instance['button_style'] == 'light') {
                echo 'selected="selected"';
            }
            ?>
>Light</option>
          <option value="dark" <?php 
            if ($instance['button_style'] == 'dark') {
                echo 'selected="selected"';
            }
            ?>
>Dark</option>
          <option value="flat" <?php 
            if ($instance['button_style'] == 'flat') {
                echo 'selected="selected"';
            }
            ?>
>Flat Light</option>
          <option value="flat-dark" <?php 
            if ($instance['button_style'] == 'flat-dark') {
                echo 'selected="selected"';
            }
            ?>
>Flat Dark</option>
          <option value="none" <?php 
            if ($instance['button_style'] == 'none') {
                echo 'selected="selected"';
            }
            ?>
>None</option>
        </select>
      </p>
    <?php 
        }
    }
    public function create_admin_page()
    {
        $tab = isset($_GET['tab']) ? $_GET['tab'] : 'homepage';
        // Retrieve all stored options from the database
        $this->_options = get_option('instamojo_credentials');
        // Get the Auth Token from the options
        $auth_token = $this->_options['auth_token'];
        // Check if the submit button was pressed and the form was submitted
        if (isset($_POST['submit'])) {
            if (function_exists('current_user_can') && !current_user_can('manage_options')) {
                // Die if current user cannot manage options
                die(__('Cheatin&#8217; uh?'));
            }
            // Get the POST data out
            $instamojo_credentials = $_POST['instamojo_credentials'];
            // Check if any data was sent
            if (isset($instamojo_credentials)) {
                if (isset($auth_token)) {
                    // Revoke token if Auth Token already exists
                    $this->revoke_token($auth_token);
                }
                // Create new instance to interact with Instamojo API
                $instance = new Instamojo(APPLICATION_ID, $instamojo_credentials['username'], $instamojo_credentials['password']);
                try {
                    $auth = $instance->apiAuth();
                    $instamojo_credentials['auth_token'] = $auth['token'];
                    unset($instamojo_credentials['password']);
                    // Update options with Username and Auth Token
                    update_option('instamojo_credentials', $instamojo_credentials);
                } catch (Exception $e) {
                    wp_cache_set('message', 'Seems like the credentials you entered were incorrect. Please try authenticating again.', 'instamojo-plugin');
                }
            }
        }
        // Check if request for revoking Auth Token was sent
        if (isset($_POST['revoke'])) {
            if (isset($auth_token)) {
                // Revoke token if Auth Token already exists
                $this->revoke_token($auth_token);
                unset($auth_token);
                echo '<div class="updated"><p>Token revoked successfully.</p></div>';
            }
        }
        $message = wp_cache_get('message', 'instamojo-plugin');
        if ($message) {
            echo '<div class="error"><p>' . $message . '</p></div>';
            wp_cache_delete('message', 'instamojo-plugin');
        }
        if (isset($auth_token) && !isset($auth) && $tab == 'homepage') {
            // Display notice if Auth Token is already stored
            echo '<div class="updated"><p>You have already authenticated your account with us. If you wish to switch accounts then enter your details again.</p></div>';
        } else {
            if (isset($auth) && $tab == 'homepage') {
                echo '<div class="updated"><p>Thanks for authenticating your account with us.</p></div>';
            }
        }
        ?>
    <div class="wrap">
      <?php 
        $this->admin_tabs();
        ?>
      <?php 
        switch ($tab) {
            case 'homepage':
                ?>
      <h3><?php 
                _e('Instamojo Credentials');
                ?>
</h3>
      <form method="post" action="" id="instamojo-conf">
        <table class="form-table">
          <tbody>
            <tr>
              <th>
                <label for="instamojo-username"><?php 
                _e('Username');
                ?>
</label>
              </th>
              <td>
                <input type="text" id="instamojo-username" name="instamojo_credentials[username]" />
              </td>
            </tr>
            <tr>
              <th>
                <label for="instamojo-password"><?php 
                _e('Password');
                ?>
</label>
              </th>
              <td>
                <input type="password" id="instamojo-password" name="instamojo_credentials[password]" />
              </td>
            </tr>
          </tbody>
        </table>
        <p class="submit">
          <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php 
                _e('Authenticate');
                ?>
" />
        </p>
      </form>

      <h3><?php 
                _e('Revoke Your Authentication Token');
                ?>
</h3>
      <form method="post" action="" id="instamojo-token-revoke">
        <p class="submit">
          <input type="submit" name="revoke" id="revoke-button" class="button button-secondary" value="<?php 
                _e('Revoke Token');
                ?>
" <?php 
                if (!(isset($auth_token) or isset($auth))) {
                    echo 'disabled';
                }
                ?>
 />
        </p>
      </form>
      <?php 
                break;
            case 'shortcode':
                if (!$auth_token) {
                    echo '<p>You need to authenticate your account first to use this feature.</p>';
                } else {
                    // Create new instance to interact with Instamojo API
                    $instamojo = new Instamojo(APPLICATION_ID);
                    $instamojo->setAuthToken($auth_token);
                    $offerObject = $instamojo->listAllOffers();
                    $offers = $offerObject['offers'];
                    ?>
      <form method="" action="" id="instamojo-shortcode-generate">
        <table class="form-table">
          <tbody>
            <tr>
              <th>
                <label for="instamojo_offer"><?php 
                    _e('Instamojo Offer');
                    ?>
</label>
              </th>
              <td>
                <select id="instamojo_offer" name="instamojo-offer">
                  <option value="none" selected="selected">None</option>
                <?php 
                    foreach ($offers as $offer) {
                        ?>
                  <option value="<?php 
                        echo $offer['slug'];
                        ?>
"><?php 
                        echo $offer['title'];
                        ?>
</option>
                <?php 
                    }
                    ?>
                </select>
              </td>
            </tr>
            <tr>
              <th>
                <label for="instamojo_style"><?php 
                    _e('Button Style');
                    ?>
</label>
              </th>
              <td>
                <select id="instamojo_style" name="button-style">
                  <option value="none" selected="selected">None</option>
                  <option value="light">Light</option>
                  <option value="dark">Dark</option>
                  <option value="flat">Flat Light</option>
                  <option value="flat-dark">Flat Dark</option>
                </select>
              </td>
            </tr>
            <tr>
              <th>
                <label for="instamojo_text"><?php 
                    _e('Button Text');
                    ?>
</label>
              </th>
              <td>
                <input type="text" id="instamojo_text" name="button-text" value="Checkout with Instamojo" />
              </td>
            </tr>
            <tr>
              <th>
                <label for="instamojo_shortcode_output"><?php 
                    _e('Shortcode');
                    ?>
</label>
              </th>
              <td>
                <textarea id="generatedShortcode" contenteditable></textarea>
              </td>
            </tr>
          </tbody>
        </table>
      </form>
      <script type="text/javascript">
        jQuery(document).ready(function() {
          generateShortcode();

          // If offer, button style or the button text are changed
          // generate shortcode and update the textarea
          jQuery('#instamojo_offer, #instamojo_style, #instamojo_text').change(function() {
            generateShortcode();
          });

          function generateShortcode() {
            var $form = jQuery('#instamojo-shortcode-generate');
            var offer = $form.find('#instamojo_offer').val();
            var style = $form.find('#instamojo_style').val();
            var text = $form.find('#instamojo_text').val();

            var output = '[instamojo';

            if (offer !== 'none') {
              output = output + ' offer="' + offer + '"';
            }
            output = output + ' style="' + style + '"';
            output = output + ' text="' + text + '"';
            output = output + ']';

            jQuery('#generatedShortcode').text(output);
          }
        });
      </script>
      <?php 
                }
                break;
            default:
                break;
        }
        ?>
    </div>
    <?php 
    }