# Mailbox

* Trong code mẫu trên chỉ sử dụng đơn giản 1 text để hiển thị data reward.
* Việc xử lý hiển thị reward được thực hiện trong RenderReward
* Vậy nên tùy biến các biến đầu vào như thế nào là logic ở game

## 1. Điều kiện tiên quyết

* Trong game cần phải tích hợp GSM SDK: [xem hướng dẫn](https://cscmobi-gsm.gitbook.io/gsm-sdk)
* Game đã được nhập dữ liệu mail box ở GSM CMS. Phần này nếu chưa biết, thì liên hệ tuyennv (skype: nguyenvantuyen\_12a9) để được hướng dẫn

## 2. Sử dụng

* Khi sử dụng thì phía game cần phải coding thêm các logic tùy theo từng game.
* Ví dụ: Cần xử lý UI khi nhận được 1 string **`RewardData`** như việc phân tích chuỗi json từ **`RewardData`** , và hiển thị hình ảnh các phần thưởng tương ứng với logic của game.
* Ví dụ: Khi một mail được xác nhận **claim reward** thành công, thì phía game sẽ xử lý cộng phần thưởng cho user.
* Ví dụ:  Khi một mail được xác nhận gửi trạng thái **openlink** lên server thành công, thì phía game sẽ mở 1 link từ game.
* Ví dụ: Khi một mail gửi thành công 1 **input text** lên server thì phía game có thể bật 1 popup, hoặc nếu có phần thưởng thì sẽ cộng thưởng cho user.
* Ví dụ: Khi một mail gửi 1 **input text** lên server bị lỗi, thì phía game bật popup hiển thị lỗi.
* Ví dụ: Khi một mail bị xóa, thì sẽ xử lý ghi log hoặc làm gì đó.

### 2.1 Tải Plugin và import vào Unity

* Tìm plugin có tên: `MailBox [version].unitypackage`
* [Download tại đây](https://gitlab.com/cscmobistudios/gsm-sdk/-/tree/main/Unity/CSCMobiPlugin/MailBox)

### 2.2 Sử dụng prefab MailBoxPanel

* Nơi chứa Prefab: **Assets** => **CSCMobiPlugin** => **MailBox** => **MailBoxPanel.prefab**

Code mẫu:

```csharp
using GSM;
using UnityEngine;

public class MailBoxController : MonoBehaviour
{
    // Start is called before the first frame update
    public MailBoxPanel prefab;

    public void ShowMailBox()
    {
        
        //Khởi tạo mailboxPanel
        var mailboxPanel = Instantiate(prefab, this.transform);

        //Xử lý logic
        mailboxPanel.SetOnRenderReward((mailboxRewardContent, mailboxItem) =>
        {
            //Xử lý Render UI Reward từ mailboxItem.reward
            //Phía game tự phân tích và xử lý tùy ý theo dữ liệu của mailboxItem.reward (có thể là 1 json)
            //Bên dưới chỉ là 1 demo RenderReward
            mailboxRewardContent.RenderReward(mailboxItem);
        });
        mailboxPanel.SetOnMailDeleted((mailboxItem) =>
        {
            Debug.Log($"{mailboxItem.title} is deleted");
        });
        mailboxPanel.SetOnSubmitInputSuccess((mailboxItem, inputText) =>
        {
            //Xử lý khi gửi inputText lên server thành công
            if (!string.IsNullOrEmpty(mailboxItem.reward))
            {
                //Xử lý cộng thưởng cho user, nếu có dữ liệu mailboxItem.reward
            }
        });
        mailboxPanel.SetOnSubmitInputError((mailboxItem, inputText, error) =>
        {
            //Xử lý khi có lỗi xảy ra
            Debug.LogError(error);

        });
        mailboxPanel.SetOnClaimRewardSuccess((mailboxItem) =>
        {
            Debug.Log($"{mailboxItem.title} is claimed");
            if (!string.IsNullOrEmpty(mailboxItem.reward))
            {
                //Xử lý cộng thưởng cho user, nếu có dữ liệu mailboxItem.reward
            }
            //Something code
        });
        mailboxPanel.SetOnOpenLinkSuccess((mailboxItem) =>
        {
            Debug.Log($"OpenLink: {mailboxItem.link}");
            //Mở 1 Url
            Application.OpenURL(mailboxItem.link);
        });
        //Hiển thị popup mailbox
        mailboxPanel.Show();

    }
}
```

#### 2.2.1 Bắt buộc phải xử lý logic render UI Reward

* Mặc định code chỉ hiển thị 1 text hiển thị data reward mà user nhận được
* Phía game cần tự coding xử lý lại UI reward mà user có thể nhận được.
* Mục đích hiển thị các phần thưởng user có thể nhận được khi claim, hoặc submit input text lên server

<figure><img src="/files/pg7PWCub4jBAkTjEaQE3" alt=""><figcaption></figcaption></figure>

Hiện tại có một Prefab **`RewardContent`** chỉ đơn giản có 1 text hiển thị **`RewardData`** và sử dụng Script **`MailBoxRewardContent`**

<figure><img src="/files/5DHx47txBmSKBhAsvse8" alt=""><figcaption></figcaption></figure>

* Việc của developer là sửa lại UI cho prefab RewardContent để hiển thị được nội dung reward cho user thấy.
* Khi thêm bất kì biến hoặc code xử lý cho script **`MailBoxRewardContent`** thì sau đó sẽ xử lý nội dung reward tại `mailboxPanel.SetOnRenderReward`.

Code xử lý:

```csharp
mailboxPanel.SetOnRenderReward((mailboxRewardContent, mailboxItem) =>
{
    //Xử lý Render UI Reward từ mailboxItem.reward
    //Phía game tự phân tích và xử lý tùy ý theo dữ liệu của mailboxItem.reward (có thể là 1 json)
    //Bên dưới chỉ là 1 demo RenderReward
    mailboxRewardContent.RenderReward(mailboxItem);
});
```

#### 2.2.2 Xử lý Claim Reward

<figure><img src="/files/ybLdkzhtYMrhzEaeJeCm" alt=""><figcaption></figcaption></figure>

* Khi user nhấn nút **Claim** thành công thì một Action trong **`SetOnClaimRewardSuccess`** sẽ được thực hiện.
* Phía game cần xử lý logic để cộng thưởng cho user

Code xử lý:

```csharp
mailboxPanel.SetOnClaimRewardSuccess((mailboxItem) =>
 {
     Debug.Log($"{mailboxItem.title} is claimed");
     if (!string.IsNullOrEmpty(mailboxItem.reward))
     {
         //Xử lý cộng thưởng cho user, nếu có dữ liệu mailboxItem.reward
     }
     //Something code
 });
```

#### 2.2.3 Xử lý logic khi Submit Text thành công lên server

<figure><img src="/files/8qgKuvqaJxV2qkF6oY4i" alt=""><figcaption></figcaption></figure>

* Khi user nhập text vào ô `Input here...` Sau đó nhấn nút **Submit**
* Lúc này một Action trong **`SetOnSubmitInputSuccess`** sẽ được thực hiện

Code xử lý:

```csharp
mailboxPanel.SetOnSubmitInputSuccess((mailboxItem, inputText) =>
{
    //Xử lý khi gửi inputText lên server thành công
    if (!string.IsNullOrEmpty(mailboxItem.reward))
    {
        //Xử lý cộng thưởng cho user, nếu có dữ liệu mailboxItem.reward
    }
});
mailboxPanel.SetOnSubmitInputError((mailboxItem, inputText, error) =>
{
    //Xử lý khi có lỗi xảy ra
    Debug.LogError(error);

});
```

#### 2.2.3 Xử lý Open Link

<figure><img src="/files/8whKmKTNhLMxDS1wiaBN" alt=""><figcaption></figcaption></figure>

* Khi user nhấn OpenLink nếu thành công thì một action trong SetOnOpenLinkSuccess sẽ được thực hiện
* Phía game sẽ xử lý logic mở đến một link nhận được từ MailBoxItem

Code xử lý:

```csharp
mailboxPanel.SetOnOpenLinkSuccess((mailboxItem) =>
{
    Debug.Log($"OpenLink: {mailboxItem.link}");
    //Mở 1 Url
    Application.OpenURL(mailboxItem.link);
});
```

## 3. Custom RewardContent Prefab

* Nếu muốn tự tạo riêng 1 RewardContent Prefab thì có thể sử dụng cách này.
* Dưới đây chỉ là demo hướng dẫn sử dụng, chứ không bắt buộc phải tạo class với tên y hệt.

### 3.1 Tạo một class CustomRewardContent

* Cần thực thi interface **`IMailBoxRewardContent`**
* Cần kế thừa class **`MonoBehaviour`**

Code mẫu:

```csharp
using GSM;
using GSM.Models;
using TMPro;
using UnityEngine;
public class CustomRewardContent : MonoBehaviour, IMailBoxRewardContent
{
    [SerializeField] TMP_Text rewardContentText;
    public void RenderReward(MailBoxItem mailBoxItem)
    {
        rewardContentText.SetText(mailBoxItem.reward);
    }

}
```

* Trong code mẫu trên chỉ sử dụng đơn giản 1 text để hiển thị data reward.
* Việc xử lý hiển thị reward được thực hiện trong RenderReward
* Vậy nên tùy biến các biến đầu vào như thế nào là logic ở game

### 3.2 Tạo một Prefab sử dụng script CustomRewardContent

<figure><img src="/files/45M1GUFFYapdOyNLIRRL" alt=""><figcaption></figcaption></figure>

### 3.3 Sửa lại class MailBoxPanel

```csharp
namespace GSM
{
    public class MailBoxPanel : BaseMailBoxPanel<GSMMailBoxItem, CustomRewardContent>
    {
    }
}
```

### 3.4 Kéo prefab CustomRewardContent vào MailBoxPanel

* Click vào:  **`CSCMobiPlugin`**` ``=>`` `**`MailBox`**` ``=>`` `**`MailBoxPanel Prefab`**

<figure><img src="/files/9cmPTVkuIcBZ4RkJyzgG" alt=""><figcaption></figcaption></figure>

* Ở Hierarchy click vào GameObject MailBoxPanel
* Ở Inspector kéo CustomRewardContent Prefab vào thuộc tính MailBoxRewardContent Prefab

<figure><img src="/files/zV6D4wjN0rjjLz82GAik" alt=""><figcaption></figcaption></figure>

### 3.4 Hoàn thành và chạy thử

* Tùy theo dữ liệu mail của game mà sẽ nhận được danh sách các mail tương ứng
* dưới đây chỉ là dữ liệu demo

<figure><img src="/files/SAvsLy8lg83vgR4UpQk5" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.cscmobicorp.com/cscplugin/mailbox.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
