"""Change api server"""
priority = 0
from tqdm import tqdm

import json
import requests


def apply(config: dict) -> bool:
    response = requests.get(config['server'])
    assert response.status_code == 200, f"Failed to fetch data {response.status_code} {response.text}"
    for item in json.loads(response.text)['modifications']:
        tqdm.write(f"Изменение {item['file']}")
        filepath = './decompiled/smali_classes2/com/swiftsoft/anixartd/network/api/'+item['file']
        with open(filepath, 'r') as f:
            content = f.read()
        with open(filepath, 'w') as f:
            if content.count(item['src']) == 0:
                tqdm.write(f"⚠ Не найдено {item['src']}")
            f.write(content.replace(item['src'], item['dst']))
    return True
