# -*- coding: utf-8 -*-
"""Handles item replacement"""

from maya import cmds # pylint: disable=import-error
import ramses as ram

from .utils_attributes import listRamsesNodes
from .utils_update import update
from .import_geo import importGeo
from .import_anim import importAnim
from .import_rig import importRig
from .import_standard import importStandard
from .utils_constants import (
    GEO_PIPE_FILE,
    PROXYGEO_PIPE_FILE,
    GEOREF_PIPE_FILE,
    PROXYGEOREF_PIPE_FILE,
    RIG_PIPE_FILE,
    ANIM_PIPE_FILE,
    ANIMREF_PIPE_FILE,
    SET_PIPE_FILE,
    SETREF_PIPE_FILE,
    STANDARD_PIPE_FILE,
    STANDARDREF_PIPE_FILE,
    MA_FILE,
    MB_FILE,
    ABC_FILE,
    VPSHADERS_PIPE_FILE,
    RDRSHADERS_PIPE_FILE,
    PROXYSHADE_PIPE_FILE,
)
from .utils_attributes import(
    RamsesAttribute,
    getRamsesAttr
)

def checkType(filePath):
    """Checks the type of pipe for the given file path.
    Returns a tuple (type:str, isRef:Bool)"""
    importType = 'Standard'
    isRef = False
    if GEO_PIPE_FILE.check(filePath):
        importType = 'Geo'
    elif PROXYGEO_PIPE_FILE.check(filePath):
        importType = 'Geo'
    elif GEOREF_PIPE_FILE.check(filePath):
        importType = 'Geo'
        isRef = True
    elif PROXYGEOREF_PIPE_FILE.check(filePath):
        importType = 'Geo'
        isRef = True
    elif RIG_PIPE_FILE.check(filePath):
        importType = 'Rig'
        isRef = True
    elif ANIM_PIPE_FILE.check(filePath):
        importType = 'Anim'
    elif ANIMREF_PIPE_FILE.check(filePath):
        importType = 'Anim'
        isRef = True
    elif SET_PIPE_FILE.check(filePath):
        importType = 'Set'
    elif SETREF_PIPE_FILE.check(filePath):
        importType = 'Set'
        isRef = True
    elif STANDARD_PIPE_FILE.check(filePath):
        importType = 'Standard'
    elif STANDARDREF_PIPE_FILE.check(filePath):
        importType = 'Standard'
        isRef = True
    elif MA_FILE.check(filePath) or MB_FILE.check(filePath) or ABC_FILE.check(filePath):
        importType = 'Standard'
    elif VPSHADERS_PIPE_FILE.check(filePath):
        importType = 'vpSha'
        isRef = True
    elif RDRSHADERS_PIPE_FILE.check(filePath):
        importType = 'rdrSha'
        isRef = True
    elif PROXYSHADE_PIPE_FILE.check(filePath):
        importType = 'proxySha'

    return (importType, isRef)

def replacer(item, filePath, step):
    """Runs a few checks and replaces selected nodes with the ones from the filepath"""

    ram.log("I'm replacing the selected nodes with " + str(item))

    # Keep the list of selected Ramses nodes (the selection changes when importing)
    originalNodes = listRamsesNodes(selectedOnly=True)

    # Check the type of the new pipe file
    importType = checkType(filePath)
    isRef = importType[1]
    importType = importType[0]

    # NOTE: Shaders are not supported for replacement yet
    if importType in ('vpSha', 'rdrSha', 'proxySha'):
        ram.log( "I can't replace shaders, sorry." )
        cmds.inViewMessage(
            msg='<hl>Sorry, shader replacement is not supported.</hl>',
            pos='midCenter',
            fade=True )
        return

    # Check types
    for originalNode in originalNodes:
        sourceFile = getRamsesAttr(originalNode, RamsesAttribute.SOURCE_FILE)
        originalType = checkType(sourceFile)[0]
        if originalType != importType:
            # Replacing with a different type
            result = cmds.confirmDialog(
                message= "Warning: you're trying to replace with a different type.\n\n" +
                "Replacing: " + originalType + "\nWith: " + importType + "\n\n" +
                "Are you sure you want to continue?",
                button=("Continue", "Cancel")
                )
            if result == 'Cancel':
                ram.log( "Cancelled." )
                return
            break

    # for each selected node, import, update and delete previous
    for originalNode in originalNodes:
        newRootCtrls = []
        # import
        if importType == 'Geo':
            newRootCtrls = importGeo(item, filePath, step, ref=isRef )
        elif importType == 'Anim':
            newRootCtrls = importAnim(item, filePath, step, showDialog=False, ref=isRef)
        elif importType == 'Set':
            newRootCtrls = importGeo(item, filePath, step, ref=isRef)
        elif importType == 'Rig':
            newRootCtrls = importRig( item, filePath, step)
        elif importType == 'Standard':
            newRootCtrls = importStandard( item, filePath, step, ref=isRef)

        # Nothing was imported
        if len(newRootCtrls) == 0:
            continue

        # Snap and re-apply sets
        newRootCtrls = update(originalNode, newRootCtrls)

        ram.log("I've replaced: " + originalNode)

        # Delete the old hierarchy and the locator
        cmds.delete( originalNode )
        