package com.kdanmobile.kdan_others_library_for_android.card;

import android.content.Context;

import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kdanmobile.kdan_others_library_for_android.R;
import com.kdanmobile.kdan_others_library_for_android.model.AnimatorHelper;
import com.kdanmobile.kdan_others_library_for_android.model.StorageHelper;
import com.kdanmobile.kdan_others_library_for_android.view.PercentageCircleView;


public class AccountInfoCard extends LinearLayout {

    private View loginView;
    private View infoView;
    private TextView emailTextView;
    private TextView storageTextView;
    private TextView percentageTextView;
    private PercentageCircleView usagePercentageCircleView;
    private Button loginButton;
    private Button registerButton;
    private Button upgradeButton;
    private Button detailButton;


    public AccountInfoCard(Context context) {
        super(context);
        initView();
    }

    public AccountInfoCard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public AccountInfoCard(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View view = inflate(getContext(), R.layout.widget_account_info_card, this);

        loginView = view.findViewById(R.id.view_account_info_card_login);
        infoView = view.findViewById(R.id.view_account_info_card_info);
        emailTextView = (TextView) view.findViewById(R.id.textView_account_info_card_email);
        storageTextView = (TextView) view.findViewById(R.id.textView_account_info_card_storage);
        percentageTextView = (TextView) view.findViewById(R.id.textView_account_info_card_percentage);
        usagePercentageCircleView = (PercentageCircleView) view.findViewById(R.id.percentageCircleView_account_info_card_usage);
        loginButton = (Button) view.findViewById(R.id.button_account_info_card_login);
        registerButton = (Button) view.findViewById(R.id.button_account_info_card_register);
        upgradeButton = (Button) view.findViewById(R.id.button_account_info_card_upgrade);
        detailButton = (Button) view.findViewById(R.id.button_account_info_card_detail);
    }

    public void update(Data data) {
        if (data.isLoggedIn) {
            loginView.setVisibility(GONE);
            infoView.setVisibility(VISIBLE);
            emailTextView.setText(data.email);
            storageTextView.setText(getContext().getString(R.string.space_used_condition, StorageHelper.covertByteUnit(data.usedStorage), StorageHelper.covertByteUnit(data.fullStorage)));
            usagePercentageCircleView.setEmptyColor(0x4DFFFFFF);
            usagePercentageCircleView.setCoverColor(ContextCompat.getColor(getContext(),android.R.color.primary_text_dark));
            AnimatorHelper animatorHelper = new AnimatorHelper();
            animatorHelper.startCirclePercentAnimator(0, new Float(data.fullStorage == 0 ? 0 : (float) data.usedStorage * 100 / (float) data.fullStorage),
                    updatePercent -> {
                        usagePercentageCircleView.setPercent(updatePercent / 100);
                        percentageTextView.setText(String.format("%.0f", updatePercent) + " %");
                    });
            if (data.isSubscribedCreative365) {
                upgradeButton.setVisibility(GONE);
            } else {
                upgradeButton.setVisibility(VISIBLE);
            }
        } else {
            loginView.setVisibility(VISIBLE);
            infoView.setVisibility(GONE);
        }
    }

    public void setOnClickLoginBtnListener(OnClickListener l) {
        loginButton.setOnClickListener(l);
    }

    public void setOnClickRegisterBtnListener(OnClickListener l) {
        registerButton.setOnClickListener(l);
    }

    public void setOnClickUpgradeBtnListener(OnClickListener l) {
        upgradeButton.setOnClickListener(l);
    }

    public void setOnClickDetailButtonListener(OnClickListener l) {
        detailButton.setOnClickListener(l);
    }

    public static class Data {
        public boolean isLoggedIn;
        public String email;
        public long usedStorage = 0;
        public long fullStorage = 0;
        public boolean isSubscribedCreative365 = false;
    }
}
