"""Constants for all image resources."""

__author__ = "rishisharma@google.com (Rishi Sharma)"

import logging
import os
import types

# pylint: disable=g-bad-import-order
import wx

from common import memoize
from controls import colors
from resources import resource_file

LOGGER = logging.getLogger(__name__)

def _GetRelativeResourceImagePath(image):
  """Returns the absolute path of the images."""

  return os.path.join("images", image)

# Material Assets
CHECKBOX_CHECKED_ENABLED = _GetRelativeResourceImagePath(
    "md_checkbox_checked_enabled.png")
CHECKBOX_CHECKED_DISABLED = _GetRelativeResourceImagePath(
    "md_checkbox_checked_disabled.png")
CHECKBOX_UNCHECKED_ENABLED = _GetRelativeResourceImagePath(
    "md_checkbox_unchecked_enabled.png")
CHECKBOX_UNCHECKED_DISABLED = _GetRelativeResourceImagePath(
    "md_checkbox_unchecked_disabled.png")
CHECKBOX_UNDETERMINED_ENABLED = _GetRelativeResourceImagePath(
    "md_checkbox_undetermined_enabled.png")
CHECKBOX_UNDETERMINED_DISABLED = _GetRelativeResourceImagePath(
    "md_checkbox_undetermined_disabled.png")
RADIOBUTTON_CHECKED_ENABLED = _GetRelativeResourceImagePath(
    "md_radiobutton_checked_enabled.png")
RADIOBUTTON_UNCHECKED_ENABLED = _GetRelativeResourceImagePath(
    "md_radiobutton_unchecked_enabled.png")
RADIOBUTTON_CHECKED_DISABLED = _GetRelativeResourceImagePath(
    "md_radiobutton_checked_disabled.png")
RADIOBUTTON_UNCHECKED_DISABLED = _GetRelativeResourceImagePath(
    "md_radiobutton_unchecked_disabled.png")

# Dialog related images.
DIALOG_SETUP_WELCOME = _GetRelativeResourceImagePath("setup_welcome.png")

DIALOG_SETUP_DRIVE_FOLDER_MAC = _GetRelativeResourceImagePath(
    "setup_drive_folder_mac.png")
DIALOG_SETUP_DRIVE_FOLDER_WIN = _GetRelativeResourceImagePath(
    "setup_drive_folder_win.png")
DIALOG_SETUP_DRIVE_FOLDER_LIN = _GetRelativeResourceImagePath(
    "setup_drive_folder_lin.png")

DIALOG_SETUP_MOBILE_BOTTOM = _GetRelativeResourceImagePath(
    "setup_mobile_bottom.png")
DIALOG_SETUP_MOBILE_RIGHT = _GetRelativeResourceImagePath(
    "setup_mobile_right.png")

DIALOG_SETUP_RIGHT_CLICK_MAC = _GetRelativeResourceImagePath(
    "setup_right_click_mac.png")
DIALOG_SETUP_RIGHT_CLICK_WIN = _GetRelativeResourceImagePath(
    "setup_right_click_win.png")

DIALOG_SETUP_TASKBAR_MAC = _GetRelativeResourceImagePath(
    "setup_taskbar_mac.png")
DIALOG_SETUP_TASKBAR_WIN = _GetRelativeResourceImagePath(
    "setup_taskbar_win.png")
DIALOG_SETUP_TASKBAR_LIN = _GetRelativeResourceImagePath(
    "setup_taskbar_lin.png")

DIALOG_INFO1_WIN7 = _GetRelativeResourceImagePath("info1-windows7.png")
DIALOG_INFO1_WINXP = _GetRelativeResourceImagePath("info1-windowsxp.png")
DIALOG_SHARE = _GetRelativeResourceImagePath("shareguyicon.png")
DIALOG_WIZARD_LOGO = _GetRelativeResourceImagePath("toprighticon.png")
DIALOG_WARNING = _GetRelativeResourceImagePath("exclaim.png")
DIALOG_LARGE_WARNING = _GetRelativeResourceImagePath("warning_128.png")

# Logo related images.
LOGO_ICON = _GetRelativeResourceImagePath("sync.ico")
LOGO_IMG = _GetRelativeResourceImagePath("sync.png")
LOGO_IMG_BIG = _GetRelativeResourceImagePath("sync_128.png")

# States for all platforms.
# Mac extensions are omitted to work with NSImage.imageNamed_().
TASKBAR_ANIMATION_1_MAC = "mac-animate1"
TASKBAR_ANIMATION_2_MAC = "mac-animate2"
TASKBAR_ANIMATION_3_MAC = "mac-animate3"
TASKBAR_ANIMATION_4_MAC = "mac-animate4"
TASKBAR_ANIMATION_5_MAC = "mac-animate5"
TASKBAR_ANIMATION_6_MAC = "mac-animate6"
TASKBAR_ANIMATION_7_MAC = "mac-animate7"
TASKBAR_ANIMATION_8_MAC = "mac-animate8"
TASKBAR_ERROR_MAC = "mac-error"
TASKBAR_INACTIVE_MAC = "mac-inactive"
TASKBAR_NORMAL_MAC = "mac-normal"
TASKBAR_PAUSED_MAC = "mac-paused"

TASKBAR_ANIMATION_1_LIN = _GetRelativeResourceImagePath("win-animate1.png")
TASKBAR_ANIMATION_2_LIN = _GetRelativeResourceImagePath("win-animate2.png")
TASKBAR_ANIMATION_3_LIN = _GetRelativeResourceImagePath("win-animate3.png")
TASKBAR_ANIMATION_4_LIN = _GetRelativeResourceImagePath("win-animate4.png")
TASKBAR_ANIMATION_5_LIN = _GetRelativeResourceImagePath("win-animate5.png")
TASKBAR_ANIMATION_6_LIN = _GetRelativeResourceImagePath("win-animate6.png")
TASKBAR_ANIMATION_7_LIN = _GetRelativeResourceImagePath("win-animate7.png")
TASKBAR_ANIMATION_8_LIN = _GetRelativeResourceImagePath("win-animate8.png")
TASKBAR_ERROR_LIN = _GetRelativeResourceImagePath("win7-error.png")
TASKBAR_INACTIVE_LIN = _GetRelativeResourceImagePath("win7-inactive.png")
TASKBAR_NORMAL_LIN = _GetRelativeResourceImagePath("win-normal.png")
TASKBAR_PAUSED_LIN = _GetRelativeResourceImagePath("win7-paused.png")

TASKBAR_ANIMATION_1_WIN = _GetRelativeResourceImagePath("win-animate1.png")
TASKBAR_ANIMATION_2_WIN = _GetRelativeResourceImagePath("win-animate2.png")
TASKBAR_ANIMATION_3_WIN = _GetRelativeResourceImagePath("win-animate3.png")
TASKBAR_ANIMATION_4_WIN = _GetRelativeResourceImagePath("win-animate4.png")
TASKBAR_ANIMATION_5_WIN = _GetRelativeResourceImagePath("win-animate5.png")
TASKBAR_ANIMATION_6_WIN = _GetRelativeResourceImagePath("win-animate6.png")
TASKBAR_ANIMATION_7_WIN = _GetRelativeResourceImagePath("win-animate7.png")
TASKBAR_ANIMATION_8_WIN = _GetRelativeResourceImagePath("win-animate8.png")
TASKBAR_ERROR_WIN7 = _GetRelativeResourceImagePath("win7-error.png")
TASKBAR_ERROR_WINXP = _GetRelativeResourceImagePath("winxp-error.png")
TASKBAR_INACTIVE_WIN7 = _GetRelativeResourceImagePath("win7-inactive.png")
TASKBAR_INACTIVE_WINXP = _GetRelativeResourceImagePath("winxp-inactive.png")
TASKBAR_NORMAL_WIN = _GetRelativeResourceImagePath("win-normal.png")
TASKBAR_PAUSED_WIN7 = _GetRelativeResourceImagePath("win7-paused.png")
TASKBAR_PAUSED_WINXP = _GetRelativeResourceImagePath("winxp-paused.png")

# Menu sync animation frames
MENU_ANIMATION_SYNC_00 = _GetRelativeResourceImagePath("sync_anim_00.png")
MENU_ANIMATION_SYNC_01 = _GetRelativeResourceImagePath("sync_anim_01.png")
MENU_ANIMATION_SYNC_02 = _GetRelativeResourceImagePath("sync_anim_02.png")
MENU_ANIMATION_SYNC_03 = _GetRelativeResourceImagePath("sync_anim_03.png")
MENU_ANIMATION_SYNC_04 = _GetRelativeResourceImagePath("sync_anim_04.png")
MENU_ANIMATION_SYNC_05 = _GetRelativeResourceImagePath("sync_anim_05.png")
MENU_ANIMATION_SYNC_06 = _GetRelativeResourceImagePath("sync_anim_06.png")
MENU_ANIMATION_SYNC_07 = _GetRelativeResourceImagePath("sync_anim_07.png")
MENU_ANIMATION_SYNC_08 = _GetRelativeResourceImagePath("sync_anim_08.png")
MENU_ANIMATION_SYNC_09 = _GetRelativeResourceImagePath("sync_anim_09.png")
MENU_ANIMATION_SYNC_10 = _GetRelativeResourceImagePath("sync_anim_10.png")
MENU_ANIMATION_SYNC_11 = _GetRelativeResourceImagePath("sync_anim_11.png")

# RETINA
# TODO(rishisharma): Use the bottome images for retina.
MENU_ANIMATION_SYNC_00_2X = _GetRelativeResourceImagePath(
    "sync_anim_00_2x.png")
MENU_ANIMATION_SYNC_01_2X = _GetRelativeResourceImagePath(
    "sync_anim_01_2x.png")
MENU_ANIMATION_SYNC_02_2X = _GetRelativeResourceImagePath(
    "sync_anim_02_2x.png")
MENU_ANIMATION_SYNC_03_2X = _GetRelativeResourceImagePath(
    "sync_anim_03_2x.png")
MENU_ANIMATION_SYNC_04_2X = _GetRelativeResourceImagePath(
    "sync_anim_04_2x.png")
MENU_ANIMATION_SYNC_05_2X = _GetRelativeResourceImagePath(
    "sync_anim_05_2x.png")
MENU_ANIMATION_SYNC_06_2X = _GetRelativeResourceImagePath(
    "sync_anim_06_2x.png")
MENU_ANIMATION_SYNC_07_2X = _GetRelativeResourceImagePath(
    "sync_anim_07_2x.png")
MENU_ANIMATION_SYNC_08_2X = _GetRelativeResourceImagePath(
    "sync_anim_08_2x.png")
MENU_ANIMATION_SYNC_09_2X = _GetRelativeResourceImagePath(
    "sync_anim_09_2x.png")
MENU_ANIMATION_SYNC_10_2X = _GetRelativeResourceImagePath(
    "sync_anim_10_2x.png")
MENU_ANIMATION_SYNC_11_2X = _GetRelativeResourceImagePath(
    "sync_anim_11_2x.png")

# Menu item icons
MENU_ALERT = _GetRelativeResourceImagePath("menu_warning.png")
MENU_ALERT_COLOR = _GetRelativeResourceImagePath(
    "ic_warning_grey600_24dp.png")
MENU_SYNC_PROBLEM = _GetRelativeResourceImagePath("ic_sync_problem_24.png")
MENU_SYNCING = _GetRelativeResourceImagePath("sync_menu_syncing.png")
MENU_DONE = _GetRelativeResourceImagePath("sync_menu_done.png")
MENU_ERROR = _GetRelativeResourceImagePath("sync_menu_error.png")
MENU_CHECK = _GetRelativeResourceImagePath("ic_done.png")
MENU_CREATE = _GetRelativeResourceImagePath("menu_create.png")
MENU_DOWNLOAD = _GetRelativeResourceImagePath("menu_down-arrow.png")
MENU_UPLOAD = _GetRelativeResourceImagePath("menu_up-arrow.png")
MENU_ERROR_ANIMATED = _GetRelativeResourceImagePath("menu_error.png")
MENU_FOLDER = _GetRelativeResourceImagePath("ic_folder_mydrive_24.png")
MENU_FOLDER32 = _GetRelativeResourceImagePath("menu_folder_32.png")
MENU_FILE32 = _GetRelativeResourceImagePath("menu_file_32.png")
MENU_SETTINGS = _GetRelativeResourceImagePath("ic_drawer_24.png")
MENU_WEB = _GetRelativeResourceImagePath("ic_web_24.png")
MENU_LOGO = _GetRelativeResourceImagePath("menu_drive-logo.png")
MENU_GOOG_LOGO = _GetRelativeResourceImagePath("menu_google-logo-gray.png")
MENU_LINK = _GetRelativeResourceImagePath("menu_link.png")
MENU_SHARE = _GetRelativeResourceImagePath("menu_share.png")
MENU_SHARE_DISABLED = _GetRelativeResourceImagePath("menu_share_disabled.png")
MENU_SYNC_PAUSED = _GetRelativeResourceImagePath("menu_sync-paused.png")
MENU_DOCS = _GetRelativeResourceImagePath("menu_docs_16.png")
MENU_SHEETS = _GetRelativeResourceImagePath("menu_sheets_16.png")
MENU_SLIDES = _GetRelativeResourceImagePath("menu_slides_16.png")
MENU_QUOTA_ERROR = _GetRelativeResourceImagePath("menu_sm_warning_red.png")
MENU_QUOTA_WARN = _GetRelativeResourceImagePath("menu_sm_warning_yellow.png")
EXPAND_BUTTON = _GetRelativeResourceImagePath(
    "ic_keyboard_arrow_right_black_24dp.png")
RTL_EXPAND_BUTTON = _GetRelativeResourceImagePath(
    "ic_keyboard_arrow_left_black_24dp.png")

# Windows specific icons
GDOC_ICON_WIN = _GetRelativeResourceImagePath("gdoc.ico")
GDRAW_ICON_WIN = _GetRelativeResourceImagePath("gdraw.ico")
GFORM_ICON_WIN = _GetRelativeResourceImagePath("gform.ico")
GLINK_ICON_WIN = _GetRelativeResourceImagePath("glink.ico")
GMAP_ICON_WIN = _GetRelativeResourceImagePath("gmap.ico")
GNOTE_ICON_WIN = _GetRelativeResourceImagePath("gnote.ico")
GSCRIPT_ICON_WIN = _GetRelativeResourceImagePath("gscript.ico")
GSHEET_ICON_WIN = _GetRelativeResourceImagePath("gsheet.ico")
GSITE_ICON_WIN = _GetRelativeResourceImagePath("gsite.ico")
GSLIDES_ICON_WIN = _GetRelativeResourceImagePath("gslides.ico")
GTABLE_ICON_WIN = _GetRelativeResourceImagePath("gtable.ico")

# Mac specific icons
DRIVE_FOLDER_ICON_MAC = _GetRelativeResourceImagePath(
    "folder-mac.icns")
DRIVE_FOLDER_ICON_MAC_YOSEMITE = _GetRelativeResourceImagePath(
    "folder-mac-yosemite.icns")
SHARED_FOLDER_ICON_MAC = _GetRelativeResourceImagePath(
    "sharedfolder-mac.icns")
SHARED_FOLDER_ICON_MAC_YOSEMITE = _GetRelativeResourceImagePath(
    "sharedfolder-mac-yosemite.icns")

BLACKLIST_FOLDER = _GetRelativeResourceImagePath("folder.png")
BLACKLIST_FILE = _GetRelativeResourceImagePath("file.png")
FOLDER = _GetRelativeResourceImagePath("folder.png")

SETTINGS_MYDRIVE_ENABLED_ICON = _GetRelativeResourceImagePath(
    "folder_mydrive_enabled.png")
SETTINGS_MYDRIVE_DISABLED_ICON = _GetRelativeResourceImagePath(
    "folder_mydrive_disabled.png")
SETTINGS_COMPUTER_ENABLED_ICON = _GetRelativeResourceImagePath(
    "computer_enabled.png")
SETTINGS_COMPUTER_DISABLED_ICON = _GetRelativeResourceImagePath(
    "computer_disabled.png")

SETTINGS_SELECTIVE_SYNC_LOAD_FOLDERS_SPINNER = _GetRelativeResourceImagePath(
    "selective_sync_load.gif")


@memoize.CachedMethod(expire_time_secs=None)
def LoadImage(image):
  """Loads and returns the image as a wx.Image.

  Args:
    image: Path to image, relative to root of sync source tree.

  Returns:
    wx.Image
  """
  LOGGER.info("Loading image %s", image)
  with open(resource_file.GetResourcePath(image), "rb") as image_file:
    return wx.ImageFromStream(wx.InputStream(image_file))


@memoize.CachedMethod(expire_time_secs=None)
def LoadBitmap(image):
  """Loads and returns the image as a wx.Bitmap.

  Args:
    image: Path to image, relative to the app resources folder.

  Returns:
    wx.Bitmap
  """
  return wx.BitmapFromImage(LoadImage(image))


@memoize.CachedMethod(expire_time_secs=None)
def LoadIcon(image):
  """Loads and returns the image as a wx.Icon.

  Args:
    image: Path to image, relative to app resources folder.

  Returns:
    wx.Icon
  """
  icon = wx.EmptyIcon()
  icon.CopyFromBitmap(LoadBitmap(image))
  return icon


@memoize.CachedMethod(expire_time_secs=None)
def LoadCustomImage(image, loading_function):
  """Returns the image loaded with loading_function.

  Image is cached.

  Args:
    image: Path to image, relative to app resources folder.
    loading_function: Function called to load the image from the image full
      path argument. The function is used as part of the key in the image
      cache and needs to be a static function to get the benefits of the
      cache without risking to fill it.

  Returns:
    The image loaded with loading_function.
  """
  assert isinstance(loading_function, types.FunctionType)
  LOGGER.info("Loading custom image %s with function %s",
              image, loading_function.__name__)
  return loading_function(resource_file.GetResourcePath(image))


@memoize.CachedMethod(expire_time_secs=None)
def LoadBitmapAndTransparentVersion(image, factor_alpha):
  """Loads and returns two images, the original and a transparent version.

  Args:
    image: Path to image, relative to root of sync source tree.
    factor_alpha: Factor to multiple the image alpha channel with to create
      the transparent version.

  Returns:
    A tuple (wx.Bitmap, wx.Bitmap), the first one is the original image and
    the second one is the version with the factor_alpha applied to it.
  """
  image = LoadImage(image)
  transparent_version = image.AdjustChannels(1, 1, 1, factor_alpha)
  return wx.BitmapFromImage(image), wx.BitmapFromImage(transparent_version)


def LoadHighContrastCompatibleBitmap(image):
  """Replaces all pixels of RGB Black with system-theme-compatible color."""
  img_obj = LoadImage(image)
  color = colors.Black()
  if color != wx.BLACK:
    img_obj.Replace(wx.BLACK.red, wx.BLACK.green, wx.BLACK.blue,
                    color.red, color.green, color.blue)
  return wx.BitmapFromImage(img_obj)
