import bpy

from .auth_client import get_authorization_state, has_saved_login
from .config import ADDON_ENVIRONMENT, ADDON_VERSION
from .update_client import get_update_state


class VIEW3D_PT_threedripper_helper(bpy.types.Panel):
    bl_idname = "VIEW3D_PT_threedripper_helper"
    bl_label = "3DRipper Helper"
    bl_category = "3DRipper"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout
        state = get_authorization_state()
        has_login = has_saved_login(context)

        account_box = layout.box()
        account_box.label(text="3DRipper account", icon="USER")
        status_row = account_box.row()
        if state.phase == "allowed" and state.entitlement:
            status_row.label(text="Access active", icon="CHECKMARK")
            if state.entitlement.account_name:
                account_box.label(text=state.entitlement.account_name)
            if state.entitlement.tier:
                account_box.label(text="Plan: {}".format(state.entitlement.tier))
        elif state.phase == "denied":
            status_row.alert = True
            status_row.label(text="Access unavailable", icon="ERROR")
            account_box.label(text=state.message)
        elif state.phase == "awaiting_approval":
            status_row.label(text="Awaiting browser approval", icon="TIME")
            account_box.label(text="Finish the approval request in your browser.")
            if state.user_code:
                account_box.label(text="Code: {}".format(state.user_code))
            if state.verification_uri_complete:
                open_page = account_box.operator(
                    "wm.url_open", text="Open authorization page", icon="URL"
                )
                open_page.url = state.verification_uri_complete
        elif state.phase == "checking":
            status_row.label(text="Checking account access", icon="FILE_REFRESH")
        elif has_login:
            status_row.label(text="Login saved; verify access", icon="INFO")
        else:
            status_row.label(text="Sign in required", icon="LOCKED")

        actions = account_box.row(align=True)
        if not has_login:
            actions.operator("wm.threedripper_helper_login", text="Sign In", icon="URL")
        else:
            actions.operator("wm.threedripper_helper_refresh_access", text="Refresh", icon="FILE_REFRESH")
            actions.operator("wm.threedripper_helper_logout", text="Sign Out", icon="LOOP_BACK")

        layout.separator()
        import_box = layout.box()
        import_box.label(text="Scene IR package", icon="PACKAGE")
        import_box.operator(
            "import_scene.threedripper_helper_ir",
            text="Import Scene IR Package",
            icon="IMPORT",
        )
        import_box.label(text="3DRipper verifies access before each import.")

        layout.separator()
        update_box = layout.box()
        update_box.label(text="Add-on update", icon="FILE_REFRESH")
        update_box.label(text="Installed version: {}".format(ADDON_VERSION))
        update_box.label(text="Environment: {}".format(ADDON_ENVIRONMENT.title()))
        update_state = get_update_state()
        if update_state.phase == "available" and update_state.release:
            update_box.alert = True
            update_box.label(text="Version {} is available.".format(update_state.release.latest_version))
            if update_state.release.release_notes:
                notes = " ".join(update_state.release.release_notes.split())
                update_box.label(text=notes[:140])
            if update_state.release.release_url:
                open_release = update_box.operator(
                    "wm.url_open", text="Open official update", icon="URL"
                )
                open_release.url = update_state.release.release_url
            else:
                update_box.label(text="Release link is not configured yet.")
        elif update_state.phase == "up_to_date":
            update_box.label(text=update_state.message, icon="CHECKMARK")
        elif update_state.phase == "checking":
            update_box.label(text=update_state.message, icon="FILE_REFRESH")
        elif update_state.phase == "error":
            update_box.alert = True
            update_box.label(text=update_state.message, icon="ERROR")
        update_box.operator(
            "wm.threedripper_helper_check_update", text="Check for updates", icon="FILE_REFRESH"
        )
