import requests
import json
from server import create_app

class Connect:
    def __init__(self, base_url):
        self.headers = {'Content-type': 'application/json'}
        self.token = ""
        self.login_id = ""

    def get_access_token(self):
        app = create_app()
        app.env = 'development'
        app.run(host='127.0.0.1', debug=False, port=65010)
        print('Open this url in browser:', 'http://127.0.0.1:65010/getcode', end='\n\n')

    def user_login(self, login_id, password):
        base_url = self.base_url
        headers = self.headers
        self.login_id = login_id
        data={
            'login_id': login_id,
            'password': password,
            "device": "web"
        }
        res = requests.post(f'{base_url}/api/v1/user/login', headers=headers, data=json.dumps(data))
        return res.json()

    def twofa(self, question_id, answer, twofa_token):
        login_id = self.login_id
        base_url = self.base_url
        headers = self.headers
        data= {
            'login_id': login_id,
            'twofa': [
                {
                    'question_id': question_id,
                    'answer': answer
                }
            ],
            'twofa_token': twofa_token,
            'type': 'PIN'
        }
        res = requests.post(f'{base_url}/api/v1/user/twofa', headers=headers, data=json.dumps(data))
        return res.json()

    def set_token(self, token):
        self.token = token

    def print_token(self):
        print(self.token)

    def get_request(self, url, params):
        headers = self.headers
        headers['x-authorization-token'] = self.token
        res = requests.get(f'{self.base_url}{url}' , params=params, headers=headers)
        return res.json()

    def post_request(self, url, data):
        headers = self.headers
        headers['x-authorization-token'] = self.token
        res = requests.post(f'{self.base_url}{url}', headers=headers, data=json.dumps(data))
        print(res)
        return res.json()

    def put_request(self, url, data):
        headers = self.headers
        headers['x-authorization-token'] = self.token
        res = requests.put(f'{self.base_url}{url}', headers=headers, data=json.dumps(data))
        print(res)
        return res.json()

    def delete_request(self, url, params):
        headers = self.headers
        headers['x-authorization-token'] = self.token
        res = requests.delete(f'{self.base_url}{url}' , params=params, headers=headers)
        return res.json()

    def fetch_profile(self, payload):
        params = {'client_id': payload['client_id']}
        res = self.get_request("/api/v1/user/profile", params)
        return res

    def place_order(self, payload):
        data = {
            "exchange": payload['exchange'],
            "instrument_token": payload['instrument_token'],
            "client_id": payload['client_id'],
            "order_type": payload['order_type'],
            "amo": payload['amo'],
            "price": payload['price'],
            "quantity": payload['quantity'],
            "disclosed_quantity": payload['disclosed_quantity'],
            "validity": payload['validity'],
            "product": payload['product'],
            "order_side": payload['order_side'],
            "device": "api",
            "user_order_id": payload['user_order_id'],
            "trigger_price": payload['trigger_price'],
            "execution_type": payload['execution_type']
        }
        res = self.post_request(f'/api/v1/orders', data)
        return res

    def modify_order(self, payload):
        data = {
            "exchange": payload['exchange'],
            "instrument_token": payload['instrument_token'],
            "client_id": payload['client_id'],
            "order_type": payload['order_type'],
            "price": payload['price'],
            "quantity": payload['quantity'],
            "disclosed_quantity": payload['disclosed_quantity'],
            "validity": payload['validity'],
            "product": payload['product'],
            "oms_order_id": payload['oms_order_id'],
            "trigger_price": payload['trigger_price'],
            "execution_type": payload['execution_type']
        }
        res = self.put_request("/api/v1/orders", data)
        return res

    def cancel_order(self, payload):
        params = {
            'client_id': payload['client_id'],
            'execution_type': payload['execution_type']
        }
        res = self.delete_request(f'/api/v1/orders/{payload.oms_order_id}', params)
        return res

    def fetch_scripinfo(self, payload):
        params = {
            'info': 'scrip',
            'token': payload['token']
        }
        res = self.get_request(f'/api/v1/contract/NSE', params)
        return res

    def search_scrip(self, payload):
        params = {
            'key': payload['key']
        }
        res = self.get_request(f'/api/v1/search', params)
        return res

    def fetch_pending_orders(self, payload):
        params = {
            'type': 'pending',
            'client_id': payload['client_id']
        }
        res = self.get_request(f'/api/v1/orders', params)
        return res

    def fetch_completed_orders(self, payload):
        params = {
            'type': 'completed',
            'client_id': payload['client_id']
        }
        res = self.get_request(f'/api/v1/orders', params)
        return res

    def fetch_trades(self, payload):
        params = {
            'client_id': payload['client_id']
        }
        res = self.get_request(f'/api/v1/trades', params)
        return res

    def fetch_order_history(self, payload):
        params = {
            'client_id': payload['client_id']
        }
        res = self.get_request(f'/api/v1/order/{payload.oms_order_id}/history', params)
        return res

    def fetch_live_positions(self, payload):
        params = {
            'client_id': payload['client_id'],
            'type': 'live'
        }
        res = self.get_request(f'/api/v1/positions', params)
        return res

    def fetch_netwise_positions(self, payload):
        params = {
            'client_id': payload['client_id'],
            'type': 'historical'
        }
        res = self.get_request(f'/api/v1/positions', params)
        return res

    def fetch_holdings(self, payload):
        params = {
            'client_id': payload['client_id']
        }
        res = self.get_request(f'/api/v1/holdings', params)
        return res

    def fetch_funds_v2(self, payload):
        params = {
            'client_id': payload['client_id'],
            'type': 'all'
        }
        res = self.get_request(f'/api/v2/funds/view', params)
        return res

    def fetch_funds_v1(self, payload):
        params = {
            'client_id': payload['client_id'],
            'type': 'all'
        }
        res = self.get_request(f'/api/v1/funds/view', params)
        return res

    def create_alert(self, payload):
        data = {
            'exchange': payload['exchange'],
            'instrument_token': payload['instrument_token'],
            'wait_time': payload['wait_time'],
            'condition': payload['condition'],
            'user_set_values': payload['user_set_values'],
            'frequency': payload['frequency'],
            'expiry': payload['expiry'],
            'state_after_expiry': payload['state_after_expiry'],
            'user_message': payload['user_message']
        }
        res = self.post_request(f'/api/v1/alerts', data)
        return res

    def fetch_alerts(self):
        params = {}
        res = self.get_request(f'/api/v1/alerts', params)
        return res

    def update_alert(self, payload):
        data = {
            'exchange': payload['exchange'],
            'instrument_token': payload['instrument_token'],
            'wait_time': payload['wait_time'],
            'condition': payload['condition'],
            'user_set_values': payload['user_set_values'],
            'frequency': payload['frequency'],
            'expiry': payload['expiry'],
            'state_after_expiry': payload['state_after_expiry'],
            'user_message': payload['user_message']
        }
        res = self.put_request(f'/api/v1/alerts', data)
        return res

#------------------------------------------------
#For testing
#------------------------------------------------
#
#
# if __name__ == "__main__":
#     connect = Connect("https://mimik.tradelab.in")
#     token_json = connect.user_login("******", "*********")
#     token = token_json['data']['twofa']['twofa_token']
#     token_json = connect.twofa("22", "********", token)
#     auth_token = token_json['data']['auth_token']
#     connect.set_token(auth_token)
#     print(auth_token)
#     payload={
#         "exchange": "NSE",
#         "instrument_token": 3045,
#         "client_id": "*********",
#         "order_type": "LIMIT",
#         "price": "270.8",
#         "quantity": 1,
#         "disclosed_quantity": 0,
#         "validity": "DAY",
#         "product": "MIS",
#         "oms_order_id": "**********",
#         "trigger_price": 0,
#         "execution_type": "REGULAR"
#     }
#     res = connect.modify_order(payload)
#     print(res)