import subprocess, re

from .base import Base


class NewBlock(Base):

    def __init__(self, options, *args, **kwargs):
        super().__init__(options, *args, **kwargs)
        self._block = self.options['<block-name>']

    def run(self):
        clone = (
            "git clone --depth=1 git@github.com:{}/{}.git {}"
        ).format('nio-blocks', 'block_template', self._block)
        rename_block_file = (
            "cd ./{} && mv example_block.py {}_block.py"
        ).format(self._block, self._block)
        rename_test_file = (
            "cd ./{} && mv test_example_block.py test_{}_block.py"
        ).format(self._block, self._block)
        rename_readme = (
            "cd ./{} && mv BLOCK_README.md README.md"
        ).format(self._block)
        reinit_repo = (
            'cd ./{} '
            '&& git remote remove origin '
            '&& git add -A'
            '&& git commit --amend --reset-author -m "Initial commit"'
        ).format(self._block)
        subprocess.call(clone, shell=True)
        subprocess.call(rename_block_file, shell=True)
        subprocess.call(rename_test_file, shell=True)
        subprocess.call(rename_readme, shell=True)

        self.rename_block_class(self._block)
        self.rename_test_class(self._block)
        self.rename_test_imports(self._block)

        subprocess.call(reinit_repo, shell=True)

    def rename_block_class(self, block):
        camel_block_name = block
        if '_' in block:
            camel_block_name = ''
            camel_block_array = block.split('_')
            for word in camel_block_array:
                capitalized = word.title()
                camel_block_name += capitalized
        else:
            camel_block_name = camel_block_name.title()

        with open('{}_block.py'.format(block)) as f:
            file_string = f.read()
        file_string = re.sub('Example', camel_block_name, file_string)

        with open('{}_block.py'.format(block), 'w') as f:
            f.write(file_string)

    def rename_test_class(self, block):
        camel_block_name = block
        if '_' in block:
            camel_block_name = ''
            camel_block_array = block.split('_')
            for word in camel_block_array:
                capitalized = word.title()
                camel_block_name += capitalized
        else:
            camel_block_name = camel_block_name.title()
        camel_block_name = 'Test' + camel_block_name

        with open('tests/test_{}_block.py'.format(block)) as f:
            file_string = f.read()
        file_string = re.sub('TestExample', camel_block_name, file_string)

        with open('tests/test_{}_block.py'.format(block), 'w') as f:
            f.write(file_string)

    def rename_test_import(self, block):
        camel_block_name = block
        if '_' in block:
            camel_block_name = ''
            camel_block_array = block.split('_')
            for word in camel_block_array:
                capitalized = word.title()
                camel_block_name += capitalized
        else:
            camel_block_name = camel_block_name.title()

        with open('tests/test_{}_block.py'.format(block)) as f:
            file_string = f.read()
        file_string = re.sub('Example', camel_block_name, file_string)
        file_string = re.sub(
            '..example_block.py', '{}_block.py'.format(block), file_string)

        with open('tests/test_{}_block.py'.format(block), 'w') as f:
            f.write(file_string)


        # TODO:
        # rename imports in test file
        # AFTER RENAMING FILES, CLASSES, AND IMPORTS
        # `git remote remove origin`
        # `git add -A`
        # git commit --amend --reset-author -m "Initial commit"

    # TODO: REFACTOR RENAME METHODS TO NOT HAVE SO MUCH DUPLICATE CODE
        # Add filename, what-to-replace as parameters?
        # Make seperate fxns for camel_block_name & replace_in_file?

@@ -551,26 +551,29 @@
       %22cd ./
-%7B%7D
+tests
  && mv test_
@@ -1365,24 +1365,42 @@
 hell=True)%0A%0A
+    @staticmethod%0A
     def rena
@@ -1410,30 +1410,24 @@
 block_class(
-self, 
 block):%0A    
@@ -2012,32 +2012,50 @@
 e(file_string)%0A%0A
+    @staticmethod%0A
     def rename_t
@@ -2064,22 +2064,16 @@
 t_class(
-self, 
 block):%0A
@@ -2745,24 +2745,42 @@
 le_string)%0A%0A
+    @staticmethod%0A
     def rena
@@ -2794,22 +2794,16 @@
 _import(
-self, 
 block):%0A
@@ -3380,23 +3380,22 @@
 lock
-.py
 ', '
+..
 %7B%7D_block
 .py'
@@ -3382,35 +3382,32 @@
 ck', '..%7B%7D_block
-.py
 '.format(block),
@@ -3528,245 +3528,8 @@
 g)%0A%0A
-%0A        # TODO:%0A        # rename imports in test file%0A        # AFTER RENAMING FILES, CLASSES, AND IMPORTS%0A        # %60git remote remove origin%60%0A        # %60git add -A%60%0A        # git commit --amend --reset-author -m %22Initial commit%22%0A%0A    
 # TO
@@ -3595,20 +3595,16 @@
 ODE%0A    
- 
-   
 # Add fi
@@ -3642,20 +3642,16 @@
 meters?%0A
-    
     # Ma

Rename methods working in terminal, TODO: write test