import os

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkgsl.v3.gsl_client import GslClient
from huaweicloudsdkgsl.v3.model.add_or_modify_attribute_req import AddOrModifyAttributeReq
from huaweicloudsdkgsl.v3.model.attribute_req import AttributeReq
from huaweicloudsdkgsl.v3.model.batch_set_attributes_req import BatchSetAttributesReq
from huaweicloudsdkgsl.v3.model.batch_set_attributes_request import BatchSetAttributesRequest
from huaweicloudsdkgsl.v3.model.create_attribute_request import CreateAttributeRequest
from huaweicloudsdkgsl.v3.model.disable_attribute_request import DisableAttributeRequest
from huaweicloudsdkgsl.v3.model.enable_attribute_request import EnableAttributeRequest
from huaweicloudsdkgsl.v3.model.list_attributes_request import ListAttributesRequest
from huaweicloudsdkgsl.v3.model.update_attribute_request import UpdateAttributeRequest
from huaweicloudsdkgsl.v3.region.gsl_region import GslRegion


class SimAttributesApplication:
    # 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
    # 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
    __AK = os.environ["HUAWEICLOUD_SDK_AK"]
    __SK = os.environ["HUAWEICLOUD_SDK_SK"]

    # 华为云IAM服务访问终端地址
    __IAM_ENDPOINT = "https://<IamEndpoint>"

    def __init__(self):
        pass

    @staticmethod
    def main(args):
        # 创建认证
        auth = BasicCredentials(
            ak=SimAttributesApplication.__AK,
            sk=SimAttributesApplication.__SK,
        ).with_iam_endpoint(endpoint=SimAttributesApplication.__IAM_ENDPOINT)

        gsl_client = GslClient.new_builder() \
            .with_credentials(credentials=auth) \
            .with_region(region=GslRegion.CN_NORTH_4) \
            .build()
        # 创建自定义属性
        SimAttributesApplication.__create_attribute(gsl_client)
        # 查询自定义属性列表
        SimAttributesApplication.__list_attributes(gsl_client)
        # 批量设置SIM卡自定义属性
        SimAttributesApplication.__batch_set_attributes(gsl_client)
        # 启用自定义属性
        SimAttributesApplication.__enable_attribute(gsl_client)
        # 修改自定义属性名称
        SimAttributesApplication.__update_attribute(gsl_client)
        # 禁用自定义属性
        SimAttributesApplication.__disable_attribute(gsl_client)

    @staticmethod
    def __create_attribute(gsl_client):
        cust_attr_name = "SDK测试自定义属性"
        try:
            response = gsl_client.create_attribute(
                CreateAttributeRequest(
                    body=AddOrModifyAttributeReq(
                        cust_attr_name=cust_attr_name
                    )
                )
            )
            print(response)
        except exceptions.ClientRequestException as e:
            SimAttributesApplication.__handle_exception(e)

    @staticmethod
    def __list_attributes(gsl_client):
        # 需要查询的自定义属性名,如果为空则查询全部自定义属性
        cust_attr_name = "SDK测试自定义属性"
        limit = 10
        offset = 1
        # 自定义属性状态,0表示未启用, 1表示已启用
        status = 0
        try:
            response = gsl_client.list_attributes(
                ListAttributesRequest(
                    cust_attr_name=cust_attr_name,
                    limit=limit,
                    offset=offset,
                    status=status
                )
            )
            print(response)
        except exceptions.ClientRequestException as e:
            SimAttributesApplication.__handle_exception(e)

    @staticmethod
    def __batch_set_attributes(gsl_client):
        # 需要修改自定义属性的列表请求
        attributes = []
        attribute_req1 = AttributeReq()
        attribute_req1.sim_card_id = 1234567
        attribute_req1.customer_attribute1 = "自定义属性1测试"
        attribute_req1.customer_attribute2 = "自定义属性2测试"
        attribute_req1.customer_attribute3 = "自定义属性3测试"
        attribute_req1.customer_attribute4 = "自定义属性4测试"
        attribute_req1.customer_attribute5 = "自定义属性5测试"
        attribute_req1.customer_attribute6 = "自定义属性6测试"
        attributes.append(attribute_req1)
        try:
            response = gsl_client.batch_set_attributes(
                BatchSetAttributesRequest(
                    body=BatchSetAttributesReq(
                        attributes=attributes
                    )
                )
            )
            print(response)
        except exceptions.ClientRequestException as e:
            SimAttributesApplication.__handle_exception(e)

    @staticmethod
    def __enable_attribute(gsl_client):
        attribute_id = 12
        try:
            response = gsl_client.enable_attribute(
                EnableAttributeRequest(
                    attribute_id=attribute_id
                )
            )
            print(response)
        except exceptions.ClientRequestException as e:
            SimAttributesApplication.__handle_exception(e)

    @staticmethod
    def __update_attribute(gsl_client):
        attribute_id = 12
        cust_attr_name = "SDK测试自定义属性新"
        try:
            response = gsl_client.update_attribute(
                UpdateAttributeRequest(
                    attribute_id=attribute_id,
                    body=AddOrModifyAttributeReq(
                        cust_attr_name=cust_attr_name
                    )
                )
            )
            print(response)
        except exceptions.ClientRequestException as e:
            SimAttributesApplication.__handle_exception(e)

    @staticmethod
    def __disable_attribute(gsl_client):
        attribute_id = 12
        try:
            response = gsl_client.disable_attribute(
                DisableAttributeRequest(
                    attribute_id=attribute_id
                )
            )
            print(response)
        except exceptions.ClientRequestException as e:
            SimAttributesApplication.__handle_exception(e)

    @staticmethod
    def __handle_exception(e):
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)


if __name__ == "__main__":
    SimAttributesApplication().main(any)