package me.zq.youjoin.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import me.zq.youjoin.DataPresenter;
import me.zq.youjoin.R;
import me.zq.youjoin.YouJoinApplication;
import me.zq.youjoin.model.ImageInfo;
import me.zq.youjoin.model.ResultInfo;
import me.zq.youjoin.network.NetworkManager;
import me.zq.youjoin.utils.GlobalUtils;
import me.zq.youjoin.widget.enter.EmojiFragment;
import me.zq.youjoin.widget.enter.EnterEmojiLayout;
import me.zq.youjoin.widget.enter.EnterLayout;

public class PublishActivity extends BaseActivity
        implements EmojiFragment.EnterEmojiLayout, DataPresenter.SendTweet{

    @Bind(R.id.lay_photo_container)
    LinearLayout layPhotoContainer;
    @Bind(R.id.popPhoto)
    ImageButton btnPopPhoto;
    @Bind(R.id.btn_send)
    ImageButton btnSend;

    ArrayList<String> mSelectPath;
    ArrayList<ImageInfo> mData = new ArrayList<>();
    EnterEmojiLayout enterLayout;
    EditText msgEdit;

    private boolean mFirstFocus = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yj_activity_publish);
        ButterKnife.bind(this);

        btnPopPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MultiImageSelectorActivity.startSelect(PublishActivity.this, 2, 9,
                        MultiImageSelectorActivity.MODE_MULTI);
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataPresenter.sendTweet(YouJoinApplication.getCurrUser().getId(),
                        msgEdit.getText().toString(), mData, PublishActivity.this);
            }
        });

        initEnter();

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            Window window = getWindow();
//            // Translucent status bar
//            window.setFlags(
//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            // Translucent navigation bar
//            window.setFlags(
//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//
//            SystemBarTintManager tintManager = new SystemBarTintManager(this);
//            tintManager.setStatusBarTintEnabled(true);
//            // enable navigation bar tint
//            tintManager.setNavigationBarTintEnabled(true);
//            //此处可以重新指定状态栏颜色
//            tintManager.setTintColor(R.color.colorPrimary);
//            tintManager.setStatusBarTintResource(R.color.colorPrimary);
//            tintManager.setNavigationBarTintResource(R.color.colorPrimary);
//        }
    }

    @Override
    public void onSendTweet(ResultInfo info){
        if(info.getResult().equals(NetworkManager.SUCCESS)){
            msgEdit.setText("");
            mData.clear();
            layPhotoContainer.removeAllViews();
            GlobalUtils.popSoftkeyboard(PublishActivity.this, msgEdit, false);
            Toast.makeText(PublishActivity.this, getString(R.string.send_tweet_success)
                    , Toast.LENGTH_SHORT).show();
            PublishActivity.this.finish();
        }else{
            Toast.makeText(PublishActivity.this, getString(R.string.error_network)
                    , Toast.LENGTH_SHORT).show();
        }
    }

    private void initEnter() {
        enterLayout = new EnterEmojiLayout(this, null);
        msgEdit = enterLayout.content;
        enterLayout.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enterLayout.popKeyboard();
            }
        });
        enterLayout.content.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (mFirstFocus && hasFocus) {
                    mFirstFocus = false;
                    enterLayout.popKeyboard();
                }
            }
        });

    }

    @Override
    protected void onStop() {
        enterLayout.closeEnterPanel();
        super.onStop();
    }

    @Override
    public EnterLayout getEnterLayout() {
        return enterLayout;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
//                StringBuilder sb = new StringBuilder();
                layPhotoContainer.removeAllViews();
                for (String p : mSelectPath) {
//                    sb.append(p);
//                    sb.append("\n");

                    View itemView = View.inflate(PublishActivity.this, R.layout.yj_item_publish_photo, null);
                    ImageView img = (ImageView) itemView.findViewById(R.id.img);
                    itemView.setTag(p);

                    Picasso.with(PublishActivity.this)
                            .load(new File(p))
                            .resize(200, 200)
                            .centerCrop()
                            .into(img);
                    if (layPhotoContainer != null) {
                        layPhotoContainer.addView(itemView,
                                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                        LinearLayout.LayoutParams.WRAP_CONTENT));
                    }

                    mData.add(new ImageInfo(p));
                }
                //yjPublishEdit.setText(sb.toString());
            }
        }
    }


    public static void actionStart(Context context) {
        Intent intent = new Intent(context, PublishActivity.class);
        context.startActivity(intent);
    }
}

@@ -111,24 +111,54 @@
 .os.Bundle;%0A
+import android.view.MenuItem;%0A
 import andro
@@ -2299,26 +2299,24 @@
 ter();%0A%0A
-//
         
 if (Buil
@@ -2311,340 +2311,170 @@
     
-if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.KITKAT) %7B%0A//            Window window = getWindow();%0A//            // Translucent status bar%0A//            window.setFlags(%0A//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,%0A//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
+android.support.v7.app.ActionBar actionBar = getSupportActionBar();%0A        if(actionBar != null)%7B%0A            actionBar.setDisplayHomeAsUpEnabled(true
 );%0A
-//
         
     
@@ -2473,478 +2473,187 @@
     
+%7D%0A
     
-// Translucent navigation bar%0A//            window.setFlags(%0A//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,%0A//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);%0A//%0A//            SystemBarTintManager tintManager = new SystemBarTintManager(this);%0A//            tintManager.setStatusBarTintEnabled(true);%0A//            // enable navigation bar tint%0A//            tintManager.setNavigationBarTintEnabled(true
+%7D%0A%0A%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        switch (item.getItemId()) %7B%0A            case R.id.home:%0A//                this.finish(
 );%0A
-//
     
@@ -2664,240 +2664,106 @@
     
-//%E6%AD%A4%E5%A4%84%E5%8F%AF%E4%BB%A5%E9%87%8D%E6%96%B0%E6%8C%87%E5%AE%9A%E7%8A%B6%E6%80%81%E6%A0%8F%E9%A2%9C%E8%89%B2%0A//            tintManager.setTintColor(R.color.colorPrimary);%0A//            tintManager.setStatusBarTintResource(R.color.colorPrimary);%0A//            tintManager.setNavigationBarTintResource(R.color.colorPrimary
+default:%0A                this.finish();%0A                return super.onOptionsItemSelected(item
 );%0A
-//
     

change ui