How to learn apple script properly
Hey after some exploration, I figured out the code.

And I'd like to share it with you just in case someone else might have the same need.

This code basically toggles a pre-set webpage in safari. The detailed logic can be found in the pseudo code in the question description. And I believe it can be really useful if your are involving with web-wrapped applications a lot.

set appName to "Safari"
set pageName to "&lt;+Your Page Name+&gt;"
set pageUrl to "&lt;+Your page URL+&gt;"

tell application "System Events"
    if not (exists process appName) then
        tell application appName to activate
    else if frontmost of process appName then
        # only hide application if the current tab is what we want
        # my search_tabs(pageName, pageUrl)
        set visible of process appName to (my search_tabs(pageName, pageUrl))
    else
        # still unable to give focus to desired window
        set frontmost of process appName to true
        my search_tabs(pageName, pageUrl)
    end if
end tell

on search_tabs(pageName, pageUrl)
    set tab_found to false
    set be_visible to false
    tell application "Safari"
        tell window 1
            if name of current tab starts with pageName then
                # if the current tab is already what we want then
                # return false
                return false
            end if
        end tell
        repeat with w in windows
            if name of w is not "" then --in case of zombie windows
                repeat with t in tabs of w
                    if name of t starts with pageName then
                        set tab_found to true
                        tell w to set current tab to t
                        if index of w is not 1 then
                            tell w to set index to 1
                        end if
                    end if
                end repeat
            end if
        end repeat
        # if the webpage doesn't exist
        if tab_found is false then
            tell window 1
                set current tab to (make new tab with properties {URL:pageUrl})
            end tell
        end if
        return true
    end tell
end search_tabs