#!/usr/bin/env python

import glob
import re
import os
import sys
import shutil

class Error(Exception):
   """Base class for other exceptions"""
   pass
class CapitalizationError(Error):
   """Raised when the input value is too small"""
   pass


def removeFolder(folderName):
  shutil.rmtree(folderName)


def initateComponent(folderName, extensionName):
  os.mkdir(folderName)
  f= open(folderName + "/index."+extensionName,"w+")
  header = "import " + folderName + " from " + "'./" + folderName + "';"
  f.write(header)
  f.write('\n\n')
  footer = "export default " + folderName + ";"
  f.write(footer)
  f.write('\n')
  f.close()


  fileParent = folderName + "."+extensionName
  f = open(folderName + "/" + fileParent, "w+")




  header = "import React from 'react';"
  f.write(header)
  f.write('\n\n')

  body = "function " + folderName + "() {"
  f.write(body)
  f.write('\n')
  f.write('  return null;')
  f.write('\n')
  f.write('}')
  f.write('\n\n')

  footer = "export default " + folderName + ";"
  f.write(footer)
  f.write('\n')


  f.close()
  print("React Component : " + folderName + " Created ")



try :
  folderName = sys.argv[1]
  extensionName =  'js'
  if(len(sys.argv) == 3):
    extensionName =  sys.argv[2]
  if str(folderName)[0].isupper():
    if not os.path.exists(folderName):
      initateComponent(folderName, extensionName)
    else:
      decision = raw_input(folderName + "Already Exists, override ? Y/N : ")
      if str(decision).strip().lower() == 'y':
        removeFolder(folderName)
        initateComponent(folderName, extensionName)
  else:
    raise CapitalizationError

except IndexError:
  print('Provide Folder Name')

except CapitalizationError:
  print('Folder Name must be Capitalized')


