#!/usr/bin/env python3
import os
import json
import argparse
import requests

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

class EmblemSmokeTest(object):
    def __init__(self, server, username, password):
        self.server = server
        self.username = username
        self.password = password

    def do_test_feature_post(self):
        k = '0074252612205'
        url = self.server + '/api/v1/code-feature-roi/?code=' + k
        r = requests.post(url, headers={
                'Authorization': 'token ' + self.token,
            },
            files={
                'file': open(os.path.join(BASE_DIR, '../api/tests/data/0074252612205-roi.jpg'), 'rb'),
            },
            data={
                'code': '0074252612205',
            },
        )
        assert(r.status_code == 200)
        print(r)

    def do_test_feature_get(self):
        k = '0074252612205'
        url = self.server + '/api/v1/code-feature-roi/?code=' + k
        r = requests.get(url, headers={
                'Authorization': 'token ' + self.token,
            })
        assert(r.status_code == 200)
        print(r)
        print("feature roi data size:", len(r.content))

    def do_test_qr_verify(self, qrcode, should_pass=True, fn=None, angle=None):
        url = self.server + '/api/v1/qr-verify/'
        if not fn:
            fn = os.path.join(BASE_DIR, '../api/tests/data/smoketest.jpg')
        with open(fn, 'rb') as f:
            r = requests.post(url, files={
                    'photo': f,
                },
                data={
                    'qrcode': qrcode,
                    'angle': angle,
                }).json()
        print(r)
        if should_pass:
            assert r.get("id")
            assert r.get("serial_code") in qrcode
        else:
            assert 'id' not in r
            assert 'serial_code' not in r
            assert 'error' in r

    def do_test_tenant_login(self):
        url = self.server + '/api/v1/login/'
        r = requests.post(url,
            json={
                'username': 't1',
                'password': 'p1',
            }).json()
        print(r)
        assert r.get("token")

    def auth(self):
        url = self.server + '/api/v1/login/'
        r = requests.post(url, json={
            'username': self.username,
            'password': self.password,
            })
        print(r)
        r = r.json()
        print(r)
        self.token = r['token']

    def run(self):
        self.do_test_tenant_login()
        self.auth()
        self.do_test_feature_post()
        self.do_test_feature_get()
        code = '0074253255108'
        qrcode = f'https://themblem.com/api/mini-prog-entry/?code={code}'
        self.do_test_qr_verify(qrcode)
        qrcode = f'https://xy.ltd/v/{code}'
        self.do_test_qr_verify(qrcode)
        qrcode = f'https://xy.ltd/xxx/{code}'
        self.do_test_qr_verify(qrcode, should_pass=False)
        code = '1395496969703'
        qrcode = f'https://themblem.com/api/mini-prog-entry/?code={code}'
        fn = os.path.join(BASE_DIR, '../../dataset/smoke/rotated.jpg')
        self.do_test_qr_verify(qrcode, fn=fn, angle=15)
        print("all tests passed")

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--server", "-s", default="https://dev.themblem.com")
    parser.add_argument("--username", "-u", default="fam")
    parser.add_argument("--password", '-p', required=True)
    return parser.parse_args()

def main():
    args = parse_args()
    t = EmblemSmokeTest(args.server, args.username, args.password)
    t.run()

if __name__ == "__main__":
    main()
