#!/bin/bash

background_stuff() {
    # Wait past DE's display config
    sleep 1
    export DISPLAY=

    while [ "$DISPLAY" = "" ]
    do

        cd /tmp/.X11-unix && for x in X*; do
        if [ ! -e "$x" ]; then continue; fi
        export DISPLAY=":${x#X}"

        USER_NAME=$(who | awk -v vt="$DISPLAY" '$0 ~ vt {print $1}')
        USER_ID=$(id -u "$USER_NAME")
        PULSE_SERVER="unix:/run/user/"$USER_ID"/pulse/native"

        # from https://wiki.archlinux.org/index.php/Acpid#Laptop_Monitor_Power_Off
        export XAUTHORITY=$(ps -C Xorg -f --no-header | sed -n 's/.*-auth //; s/ -[^ ].*//; p')

        if [[ "$1" -eq 1 ]]
        then
            xrandr --output DP-0 --auto --output DSI-0 --off
            sudo -u "$USER_NAME" xrandr --output DP-0 --auto --output DSI-0 --off
            sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 1 off
            sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 output:hdmi-stereo
        else
            xrandr --output DSI-0 --auto --rotate left
	    xrandr --output DSI-0 --rotate left --panning 1280x720+0+0 --pos 0x0
            sudo -u "$USER_NAME" xrandr --output DSI-0 --auto --rotate left
            sudo -u "$USER_NAME" xrandr --output DSI-0 --rotate left --panning 1280x720+0+0 --pos 0x0
	    sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 1 HiFi
            sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 off
        fi
        done
        sleep 1
    done
}

if grep -q 1 "/sys/class/switch/dp/state"; then
        background_stuff 1 & disown
else
        background_stuff 0 & disown
fi
To fix switch resolution on dock remove, add

xrandr --output DSI-0 --rotate left --panning 1280x720+0+0 --pos 0x0

and sudo -u $USER_NAME xrandr --output DSI-0 --rotate left --panning 1280x720+0+0 --pos 0x0

place these lines under the previous xrandr lines in the script under else statement.

Also, for default docked profile I added xinput enable/disable 6 in the respective sections(Enable/Disable touchscreen. (not needed, but it seemed more complete. 
