from django.template.loader import render_to_string

def get_dashboard_page_content_html(request):

    from ChatServiceApp.funcs import get_unanswered_msgs_count_for_user

    Dict = {
        'unanswered_msgs_count': get_unanswered_msgs_count_for_user(request.user)
    }

    html = render_to_string('blocks/profile/b_profile_first_page.html', Dict, request=request)
    return html

def get_profile_page_content_html(request, page_name, data):

    if page_name == 'chat':
        from ChatServiceApp.funcs import get_chat_page_content_html
        return get_chat_page_content_html(request, data)
    elif page_name == 'create_route_for_customer':
        from RoutesApp.funcs import get_profile_new_route_page_html
        return get_profile_new_route_page_html(request, {'owner_type': 'customer'})
    elif page_name == 'create_route_for_mover':
        from RoutesApp.funcs import get_profile_new_route_page_html
        return get_profile_new_route_page_html(request, {'owner_type': 'mover'})
    elif page_name == 'my_routes':
        from RoutesApp.funcs import get_profile_my_routes_page_content_html
        return get_profile_my_routes_page_content_html(request)
    elif page_name == 'support':
        return get_profile_support_page_content_html(request, data)
    elif page_name == 'my_subscribe':
        from SubscribesApp.funcs import get_profile_subscribe_page_content_html
        return get_profile_subscribe_page_content_html(request)
    elif page_name == 'change_profile':
        return get_profile_change_page_content_html(request)
    elif page_name == 'dashboard':
        return get_dashboard_page_content_html(request)
    else:
        return None


def get_profile_change_page_content_html(request):

    init_Dict = {
        'firstname': request.user.first_name,
        'lastname': request.user.last_name,
        'email': request.user.email,
        'tel': request.user.user_profile.phone,
        'country': request.user.user_profile.country,
        'city': request.user.user_profile.city,
    }
    from .forms import RegistrationForm
    form = RegistrationForm(initial=init_Dict)

    from SubscribesApp.funcs import get_cur_user_subscribe

    Dict = {
        'profileForm': form,
        'user_subscribe': get_cur_user_subscribe(request.user)
    }

    html = render_to_string('blocks/profile/b_profile.html', Dict, request=request)
    return html


def get_profile_support_page_content_html(request, data=None):
    if request.user.is_staff:
        from ChatServiceApp.funcs import get_ticketsDict_for_staff
        Dict = get_ticketsDict_for_staff(request.user)
        tpl_name = 'blocks/profile/b_support_chat.html'
    else:
        from ChatServiceApp.models import MsgGroup
        tickets = MsgGroup.objects.filter(enable=True, owner=request.user).order_by('-modifiedDT')

        Dict = {
            'tickets': tickets,
        }
        tpl_name = 'blocks/profile/b_support_tickets.html'

    html = render_to_string(tpl_name, Dict, request=request)
    return html