text: '''
  Created on 3 janv. 2022
  
  @author: slinux
  '''
  import wx 
  
  from libs.RVNpyRPC._asset import AssetType
  from wxRavenGUI.application.wxcustom.CustomUserIO import UserQuestion
  
  
  
  
  
  class RavencoreAssetRightclickPopupMenu(object):
      '''
      classdocs
      '''
  
  
      def __init__(self, parentBinding, parentFrame, assetData={}):
          '''
          Constructor
          '''
          self._menu = wx.Menu()
          
          self.popupIDS = {}
          self.popupMAP = {}
          
          
          
          self._ipfsgateway_providers = parentFrame.GetPluginSetting("Ravencore","ipfsgateway_providers")
          self._ipfsgateway_default = parentFrame.GetPluginSetting("Ravencore","ipfsgateway_default")
          
          self.parentBinding = parentBinding
          self.parent_frame = parentFrame
          
          
          self._data= assetData
          self._hasIpfs = False
          self._dataNone = False
          
          
          if assetData != None:
              if assetData.__contains__('has_ipfs'):
                  self._hasIpfs =  assetData['has_ipfs']
          else:
              self._dataNone = True
          
          self.createMenu()
          
          
      
      
     
          
      def createMenu(self):   
          self.__createItemsIds__()
          self.__createMenuItems__()
          
          
          
      
      
      def __createItemsIds__(self):
          if not self.popupIDS.__contains__("popupID1"):
              
              pptext = "popupID"
              ppcount = 1
              
              
              for p in self._ipfsgateway_providers:
                  popupID = wx.NewId()
                  _pp = pptext + str(ppcount)
                  self.popupIDS[_pp] = popupID
                  self.popupMAP[popupID] = p
                  self.parentBinding.Bind(wx.EVT_MENU, self.OnOpenIPFSWithGateway, id=popupID)
                  ppcount= ppcount+1
                  
                  
                  
      
      def __createMenuItems__(self):
          
          # make a menu
          #menu = wx.Menu()
          self._menu = wx.Menu()
          
          
          #Add Bkmrk
          #
          nid = wx.NewId()
          _addBk = self._menu.Append(nid, "Add in Bookmarks" )
          _addBk.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('addbkmrk_co'))
          self.parentBinding.Bind(wx.EVT_MENU, self.addInBkmrk, id=nid)
          
          
          
          #Navigate
          #
          nid = wx.NewId()
          _Navigate = self._menu.Append(nid, "Navigate" )
          _Navigate.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('hierarchicalLayout'))
          self.parentBinding.Bind(wx.EVT_MENU, self.navigateAsset, id=nid)
          
          # Preview
          nid = wx.NewId()
          _preview = self._menu.Append(nid, "Preview" )
          _preview.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('ressource_picture'))
          self.parentBinding.Bind(wx.EVT_MENU, self.previewIPFS, id=nid)
          
          
          
          # Copy Hash
          nid = wx.NewId()
          _copyHash = self._menu.Append(nid, "Copy Hash" )
          _copyHash.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('copy_clipboard'))
          self.parentBinding.Bind(wx.EVT_MENU, self.copyHash, id=nid)
          
          
          
          # Open items
          nid = wx.NewId()
          _opendefaultipfs = self._menu.Append(nid, "Open IPFS" )
          _opendefaultipfs.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('raven_ipfs'))
          self.parentBinding.Bind(wx.EVT_MENU, self.openDefaultIPFS, id=nid)
          
          
          
          #webresources16
          _ipfs_menu = wx.Menu()
          _ipfs_menu_item = self._menu.AppendSubMenu(_ipfs_menu, "Open IPFS with...")
          _ipfs_menu_item.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('webresources16'))
      
          pptext = "popupID" 
          ppcount = 1
          
          for p in self._ipfsgateway_providers:
              pptext = "popupID"
              _pp = pptext + str(ppcount)
              _i = _ipfs_menu.Append(self.popupIDS[_pp], p)
              _i.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('webresources16'))
              ppcount= ppcount+1
              
          
          
          self._menu.AppendSeparator()
          #export list
          #ownerlist_export
          nid = wx.NewId()
          _Navigate = self._menu.Append(nid, "Export Owner List" )
          _Navigate.SetBitmap(self.parent_frame.RessourcesProvider.GetImage('ownerlist_export'))
          self.parentBinding.Bind(wx.EVT_MENU, self.exportOwnerList, id=nid)
          
          
          #_hasIpfs =  _data['has_ipfs']
          
          
          if not self._hasIpfs:
              _ipfs_menu_item.Enable(False)
              _opendefaultipfs.Enable(False)
              _copyHash.Enable(False)
              _preview.Enable(False)
          #menu.Append(self.popupID2, "Iterate Selected")
          #menu.Append(self.popupID3, "ClearAll and repopulate")
          #menu.Append(self.popupID4, "DeleteAllItems")
          #menu.Append(self.popupID5, "GetItem")
          #menu.Append(self.popupID6, "Edit")
  
          # Popup the menu.  If an item is selected then its handler
          # will be called before PopupMenu returns.
          self.parentBinding.PopupMenu(self._menu)
          self._menu.Destroy()   
      
      
      def OnOpenIPFSWithGateway(self, event):
          #self.log.WriteText("Popup one\n")
          #print("FindItem:", self.m_listCtrl1.FindItem(-1, "Roxette"))
          #print("FindItemData:", self.m_listCtrl1.FindItemData(-1, 11))
          
          _data= self._data
          if _data['has_ipfs']:
              self.parent_frame.GetPlugin("Ravencore").OpenIPFSinWebBrowser(_data, str(self.popupMAP[event.GetId()]))
          
          
      
      
      
      def addInBkmrk(self, event):
          itemData = self._data
          
          _navItem = itemData['name']
          #print(itemData['type'])
          
          
          
          if itemData['type'] != AssetType.MAINASSET:
              
              dlg = wx.MessageDialog(self.parentBinding, 'The Asset you selected is not a main asset, add main asset in bookmark instead ?',
                                 'Confirmation',
                                 wx.YES_NO | wx.ICON_INFORMATION
                                 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
                                 )
              res=dlg.ShowModal()
              dlg.Destroy()
              
              if res==wx.ID_YES or res==wx.YES:
              
                  _navItem = itemData['parent'] 
              
              
          self.parent_frame.GetPlugin("Ravencore").AddAssetInBookmark(str(_navItem))
          
          
          dlg = wx.MessageDialog(self.parentBinding, 'Asset added in the Bookmark with Success !',
                                 'Confirmation',
                                 wx.OK | wx.ICON_INFORMATION
                                 #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
                                 )
          dlg.ShowModal()
          dlg.Destroy()
      
      
      
      def navigateAsset(self, event):
          
          if not self._dataNone:
          
              itemData = self._data
              
              _navItem = itemData['name']
              if itemData['type'] != AssetType.MAINASSET:
                  _navItem = itemData['parent'] 
              
              
          self.parent_frame.GetPlugin("Ravencore").NaviguateAsset(str(_navItem))
      
      
      
      def previewIPFS(self, event):
          if self._hasIpfs :
              _url = self._ipfsgateway_default  + self._data['ipfs_hash']
              self.parent_frame.GetPlugin("Ravencore").previewIPFS(_url)
  
      
      
      def copyHash(self, event):
          if self._hasIpfs :
              _data= self._data
              self.parent_frame.GetPlugin("Ravencore").CopyClip(_data)
    
          
      
      def openDefaultIPFS(self, event): 
          if self._hasIpfs :
              _data= self._data
              self.parent_frame.GetPlugin("Ravencore").OpenIPFSinWebBrowser(_data)
             
  
  
      def exportOwnerList(self, event):
          _includeChilds = False
          itemData = self._data
          if itemData['type'] != AssetType.MAINASSET or itemData['type'] != AssetType.SUBASSET:
              _includeChilds = UserQuestion(self.parentBinding, "Include Childs ?")
          
          _navItem = itemData['name']
          
          if _includeChilds:
              _navItem = _navItem+'*'
  
          self.parent_frame.GetPlugin("Ravencore").ExportAssetOwnerList(_navItem)
          
      def getMenu(self):
          return self._menu