package net.poksion.chorong.android.samples;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import net.poksion.chorong.android.annotation.Assemble;
import net.poksion.chorong.android.module.ModuleFactory;
import net.poksion.chorong.android.store.ObjectStore;
import net.poksion.chorong.android.store.StoreObserver;
import net.poksion.chorong.android.ui.dialog.AlertDialogActivity;
import net.poksion.chorong.android.ui.main.OneSubjectMainActivity;

public class MainActivity extends OneSubjectMainActivity {

    @Assemble ObjectStore objectStore;

    @Assemble(R.id.main_content) FrameLayout parentContentView;
    @Assemble LinearLayout buttonContainer;

    private StoreObserver<Boolean> alertDialogBtnObserver = new StoreObserver<Boolean>() {
        @Override
        protected void onChanged(Boolean aBoolean) {
            if (aBoolean) {
                Toast.makeText(MainActivity.this, "Yes clicked", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, "No clicked", Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {

        ModuleFactory.assemble(this, new MainActivityAssembler(this, container));

        parentContentView.setBackgroundColor(Color.BLACK);

        addAlertDialogSample();
    }

    @Override
    protected boolean isUsingCustomTheme() {
        return false;
    }

    @Override
    protected List<MenuInfo> getMenuInfoList() {
        List<MenuInfo> menuInfoList = new ArrayList<>();
        menuInfoList.add(new MenuInfo(R.string.menu_info));

        return menuInfoList;
    }

    @Override
    protected boolean onMenuSelected(int id) {
        if (id == R.string.menu_info) {
            Toast.makeText(this, "menu info clicked", Toast.LENGTH_LONG).show();
            return true;
        }

        return false;
    }

    private void addAlertDialogSample() {

        // button clicked event
        final String ALERT_DIALOG_CLICKED_EVENT = "alert-dialog-click-event";

        objectStore.addWeakObserver(ALERT_DIALOG_CLICKED_EVENT, alertDialogBtnObserver, false);

        Button button = new Button(this);
        button.setText(R.string.button_alert_dialog);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new AlertDialogActivity.Builder(MainActivity.this, "Sample Alert", "Sample Alert Dialog Body")
                        .clickable("yes button", "no button", ALERT_DIALOG_CLICKED_EVENT)
                        .build();

                startActivity(i);
            }
        });

        buttonContainer.addView(button);
    }
}

@@ -999,443 +999,8 @@
 %0D%0A%0D%0A
-    private StoreObserver%3CBoolean%3E alertDialogBtnObserver = new StoreObserver%3CBoolean%3E() %7B%0D%0A        @Override%0D%0A        protected void onChanged(Boolean aBoolean) %7B%0D%0A            if (aBoolean) %7B%0D%0A                Toast.makeText(MainActivity.this, %22Yes clicked%22, Toast.LENGTH_LONG).show();%0D%0A            %7D else %7B%0D%0A                Toast.makeText(MainActivity.this, %22No clicked%22, Toast.LENGTH_LONG).show();%0D%0A            %7D%0D%0A        %7D%0D%0A    %7D;%0D%0A
 %0D%0A  
@@ -1888,24 +1888,619 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    // Store Observer%0D%0A    // CAUTION%0D%0A    // In Object Store, the observer reference is managed %22weak%22.%0D%0A    // Hence should manage the observer reference.%0D%0A    private StoreObserver%3CBoolean%3E alertDialogBtnObserver = new StoreObserver%3CBoolean%3E() %7B%0D%0A        @Override%0D%0A        protected void onChanged(Boolean aBoolean) %7B%0D%0A            if (aBoolean) %7B%0D%0A                Toast.makeText(MainActivity.this, %22Yes clicked%22, Toast.LENGTH_LONG).show();%0D%0A            %7D else %7B%0D%0A                Toast.makeText(MainActivity.this, %22No clicked%22, Toast.LENGTH_LONG).show();%0D%0A            %7D%0D%0A        %7D%0D%0A    %7D;%0D%0A%0D%0A
     private 
@@ -2642,24 +2642,61 @@
 -event%22;%0D%0A%0D%0A
+        // observer for click event%0D%0A
         obje

add comment for object store observer