# -*- coding: utf-8 -*-
"""
Работа с Вконтакте через API
"""
import vk
import time
import vk_cred
import json
import requests
from parser import Parser
import pprint


class VkTest:

    def __init__(self):
        self.creds = vk_cred.Cred().read()
        self.session = vk.AuthSession(app_id=self.creds[0], user_login=self.creds[1],
                                      user_password=self.creds[2], scope=self.creds[3])
        # self.session = vk.Session(access_token=self.read_cred())
        self.api = vk.API(self.session)



    def wall_mon(self):
        wall_start = self.api.wall.get()[0]
        while True:
            time.sleep(5)
            wall_posts = self.api.wall.get()
            if wall_posts[0] != wall_start:
                print('Posts: {}, was: {}\n last:{}'.format(wall_posts[0], wall_start, wall_posts[1]['text']))
                wall_start = wall_posts[0]

    def send_message(self, text, uid=6333109):
        # Masha 6333109 self 643112
        self.api.messages.send(user_id=uid, message=text)

    def get_message(self,):
        return self.api.messages.get(out=1)

    def get_long_poll_server(self):
        # 'server': '', 'key': '', 'ts':
        serv = self.api.messages.getLongPollServer(use_ssl=0, need_pts=0)
        return serv


class LongPollVK(object):

    def __init__(self):
        self.poll_serv = VkTest().get_long_poll_server()
        self.server = self.poll_serv['server']
        self.key = self.poll_serv['key']
        self.ts = self.poll_serv['ts']

    def message(self):
        answer = requests.get('https://{}?act=a_check&key={}&ts={}&wait=25&mode=2'.format(
            self.server, self.key, self.ts
        )).json()
        return answer


def main():
    vkontakte = VkTest()

    a = Parser()
    vkontakte.send_message('{}\n{}\n{}\n'.format(
        a.hi(), a.get_weather(), a.get_exchange_rates()))


if __name__ == '__main__':
    main()
    # vkontakte = VkTest()
    # poll = LongPollVK()
    # while True:
    #     input('continue')
    #     print(poll.message())
