import requests, json
import time, random

from .api_info import *
from .prepare import get_credentials

class API:

    user_agent = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
              "(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36")
    accept_language = 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4'
    url            = 'https://www.instagram.com/'
    url_tag        = 'https://www.instagram.com/explore/tags/'
    url_like       = 'https://www.instagram.com/web/likes/%s/like/'
    url_unlike     = 'https://www.instagram.com/web/likes/%s/unlike/'
    url_comment    = 'https://www.instagram.com/web/comments/%s/add/'
    url_follow     = 'https://www.instagram.com/web/friendships/%s/follow/'
    url_unfollow   = 'https://www.instagram.com/web/friendships/%s/unfollow/'
    url_login      = 'https://www.instagram.com/accounts/login/ajax/'
    url_logout     = 'https://www.instagram.com/accounts/logout/'
    url_media_info = 'https://www.instagram.com/p/%s/?__a=1'
    url_user_info  = 'https://www.instagram.com/%s/?__a=1'

    def __init__(self):
        login, password = get_credentials()
        self.user_login = login.lower()
        self.user_password = password
        self.s = requests.Session()

        self.login()

    def login(self):
        self.s.cookies.update({'sessionid': '', 'mid': '', 'ig_pr': '1',
                               'ig_vw': '1920', 'csrftoken': '',
                               's_network': '', 'ds_user_id': ''})
        self.login_post = {'username': self.user_login,
                           'password': self.user_password}
        self.s.headers.update({'Accept-Encoding': 'gzip, deflate',
                               'Accept-Language': self.accept_language,
                               'Connection': 'keep-alive',
                               'Content-Length': '0',
                               'Host': 'www.instagram.com',
                               'Origin': 'https://www.instagram.com',
                               'Referer': 'https://www.instagram.com/',
                               'User-Agent': self.user_agent,
                               'X-Instagram-AJAX': '1',
                               'X-Requested-With': 'XMLHttpRequest'})
        r = self.s.get(self.url)
        self.s.headers.update({'X-CSRFToken': r.cookies['csrftoken']})
        time.sleep(2 * random.random())
        login = self.s.post(self.url_login, data=self.login_post,
                            allow_redirects=True)
        self.s.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
        self.csrftoken = login.cookies['csrftoken']
        time.sleep(2 * random.random())

        if login.status_code == 200:
            r = self.s.get('https://www.instagram.com/')
            finder = r.text.find(self.user_login)
            if finder != -1:
                self.login_status = True
            else:
                self.login_status = False
                print ("Can't login: Invalid login or password.")
        else:
            self.login_status = False
            print ("Can't login. Status code: %s"%(login.status_code))

    def logout(self):
        ret = self.post(self.url_logout,
                        {'csrfmiddlewaretoken': self.csrftoken})
        if ret:
            self.login_status = False
        return ret

    def like(self, media_id):
        return self.post(self.url_like % (media_id), None)

    def unlike(self, media_id):
        return self.post(self.url_unlike % (media_id), None)

    def comment(self, media_id, comment_text):
        return self.post(self.url_comment % (media_id),
                         {'comment_text': comment_text})

    def follow(self, user_id):
        ''' You can pass username or user_id of person to follow'''
        return self.post(self.url_follow % self.convert_to_id(user_id), None)

    def unfollow(self, user_id):
        ''' You can pass username or user_id of person to unfollow'''
        return self.post(self.url_unfollow % self.convert_to_id(user_id), None)

    def post(self, url, data):
        if (self.login_status):
            try:
                response = self.s.post(url, data=data)
                return response
            except:
                print ("Can't send post request to %s"%url)
                pass
        return False

    def convert_to_id(self, inp):
        ''' If input is not digit - it is a username.
            So we should convert it to user_id'''
        if type(inp) == str:
            if not inp.isdigit():
                return self.get_user_id_by_username(inp)
        return str(inp)

###### Finctions from other files ######

    def get_profile_info(self, username):
        return get_profile_info(self, username)

    def get_following(self, username):
        return get_following(self, username)

    def get_user_id_by_username(self, username):
        return get_user_id_by_username(self, username)

@@ -1087,44 +1087,234 @@
     
-login, password = get_credentials()%0A
+self.s = requests.Session()%0A%0A    def login(self, login=None, password=None):%0A        ''' If login or password is not passed function will take them from secret.txt'''%0A        if login is not None and password is not None:%0A    
     
@@ -1344,17 +1344,13 @@
 ogin
-.lower()%0A
+%0A    
     
@@ -1395,79 +1395,264 @@
     
-self.s = requests.Session()%0A%0A        self.login()%0A%0A    def login(self):
+else:%0A            login, password = get_credentials()%0A            self.user_login = login.lower()%0A            self.user_password = password%0A%0A        self.login_post = %7B'username': self.user_login,%0A                           'password': self.user_password%7D%0A
 %0A   
@@ -1857,123 +1857,8 @@
 '%7D)%0A
-        self.login_post = %7B'username': self.user_login,%0A                           'password': self.user_password%7D%0A
     

move login outside of class __init__(), add possibility to pass log and password to login()