# -*- coding: utf-8 -*-

from django.shortcuts import render
from json_render import json_render

from account.models import LensmanInfo, TourGuideInfo
from utils.redis.connect import r


def lensman_oauth(request):
    lensman_type = int(request.GET.get('lt') or 0)
    unionid = request.GET.get('unionid', '')

    try:
        lensman = LensmanInfo.objects.get(unionid=unionid)
    except LensmanInfo.DoesNotExist:
        lensman = None

    return render(request, 'page/lensman_oauth.html', {
        'lensman_type': lensman_type,
        'lensman_info': lensman and lensman.data(lensman_type),
        'modified': bool((not lensman) or (lensman and lensman.modified(lensman_type))),  # 是否可以更改信息
    })


def tourguide_oauth(request):
    unionid = request.GET.get('unionid', '')

    try:
        tourguide = TourGuideInfo.objects.get(unionid=unionid)
    except TourGuideInfo.DoesNotExist:
        tourguide = None

    return render(request, 'page/tourguide_oauth.html', {
        'tourguide_info': tourguide and tourguide.data,
        'modified': bool((not tourguide) or (tourguide and tourguide.modified)),  # 是否可以更改信息
    })


def login_qrcode(request):
    lensman_type = int(request.GET.get('lt') or 0)
    unionid = request.GET.get('unionid', '')

    data = {
        'lensman_type': lensman_type,
        'unionid': unionid,
        'token': r.token(unionid)
    }

    return json_render(request, 'page/login_qrcode.html', data)