package ai.pai.client.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.common.executors.ThreadExecutor;
import com.android.common.utils.LogHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

import ai.pai.client.R;
import ai.pai.client.activity.MessageActivity;
import ai.pai.client.activity.SystemMessageActivity;
import ai.pai.client.db.Preferences;
import ai.pai.client.utils.HttpPostTask;
import ai.pai.client.utils.UrlContainer;

public class TabMessageFragment extends BaseFragment implements View.OnClickListener{

    private ImageView systemMsgNumText;
    private ImageView commentMsgNumText;
    private ImageView thumbMsgNumText;

    private HttpPostTask fetchMessageTask;


    public interface MessageType {

        String SYSTEM = "system";
        String COMMENT = "comment";
        String THUMBUP = "thumbup";
    }

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_message,container,false);
        rootView.findViewById(R.id.layout_message_push).setOnClickListener(this);
        rootView.findViewById(R.id.layout_message_thumb).setOnClickListener(this);
        rootView.findViewById(R.id.layout_message_comment).setOnClickListener(this);
        systemMsgNumText = (ImageView)rootView.findViewById(R.id.tv_message_push_number);
        commentMsgNumText = (ImageView)rootView.findViewById(R.id.tv_message_comment_number);
        thumbMsgNumText = (ImageView)rootView.findViewById(R.id.tv_message_thumb_number);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        doFetchMessageTask();
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.layout_message_push:
                intent = new Intent(getActivity(), SystemMessageActivity.class);
                intent.putExtra("type",MessageType.SYSTEM);
                break;
            case R.id.layout_message_thumb:
                intent = new Intent(getActivity(), MessageActivity.class);
                intent.putExtra("type",MessageType.THUMBUP);
                break;
            case R.id.layout_message_comment:
                intent = new Intent(getActivity(), MessageActivity.class);
                intent.putExtra("type",MessageType.COMMENT);
                break;
        }
        startActivity(intent);
    }

    private synchronized void doFetchMessageTask(){
        if(fetchMessageTask!=null && fetchMessageTask.getStatus()== AsyncTask.Status.RUNNING){
            return;
        }
        LogHelper.d(TAG,"获取新消息数目");
        HashMap<String,String> params = new HashMap<String,String>();
        String userId = Preferences.getInstance(getActivity()).getUserId();
        if(userId.length()==0){
            return;
        }
        params.put("user_id",userId);

        fetchMessageTask = new HttpPostTask(getActivity(),params) {

            int pushNum = 0;
            int thumbNum = 0;
            int commentNum = 0;

            @Override
            protected boolean parseResponse(Context context, String response) {
                try{
                    JSONObject json = new JSONObject(response);
                    int status = json.getInt("status");
                    if(status == 200){
                        JSONObject info = json.getJSONObject("data");
                        JSONArray msgArray = info.getJSONArray("messages");
                        int len = msgArray.length();
                        for(int k = 0; k< len; k++){
                            JSONObject msgObj = msgArray.getJSONObject(k);
                            String msgType = msgObj.getString("msg_type");
                            int size =  msgObj.getInt("msg_unread_num");
                            if(msgType.equals(MessageType.COMMENT)){
                                commentNum = size;
                            }else if(msgType.equals(MessageType.SYSTEM)){
                                pushNum = size;
                            }else if(msgType.equals(MessageType.THUMBUP)){
                                thumbNum = size;
                            }
                        }
                        return true;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            protected void onPostFail(Context context) {
                super.onPostFail(context);
            }

            @Override
            protected void onPostSuccess(Context context) {
                super.onPostSuccess(context);
                systemMsgNumText.setVisibility(pushNum >0 ? View.VISIBLE : View.INVISIBLE);
                thumbMsgNumText.setVisibility(thumbNum >0 ? View.VISIBLE : View.INVISIBLE);
                commentMsgNumText.setVisibility(commentNum >0 ? View.VISIBLE : View.INVISIBLE);
//                if(pushNum >0){
//                    systemMsgNumText.setVisibility(View.VISIBLE);
//                    ViewGroup.LayoutParams layoutParams = systemMsgNumText.getLayoutParams();
//                    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    if(pushNum<10){
//                        systemMsgNumText.setText(String.valueOf(pushNum));
//                        layoutParams.width =  ViewGroup.LayoutParams.WRAP_CONTENT;
//                    }else if( pushNum< 99){
//                        systemMsgNumText.setText(String.valueOf(pushNum));
//                        layoutParams.width = DeviceUtils.dip2px(context,40);
//                    }else{
//                        systemMsgNumText.setText(R.string.more_than_99);
//                        layoutParams.width = DeviceUtils.dip2px(context,54);
//                    }
//                    systemMsgNumText.setLayoutParams(layoutParams);
//                }else{
//                    systemMsgNumText.setVisibility(View.INVISIBLE);
//                }
//                if(thumbNum >0){
//                    thumbMsgNumText.setVisibility(View.VISIBLE);
//                    ViewGroup.LayoutParams layoutParams = thumbMsgNumText.getLayoutParams();
//                    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    if(thumbNum<10){
//                        thumbMsgNumText.setText(String.valueOf(thumbNum));
//                        layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    }else if( thumbNum< 99){
//                        thumbMsgNumText.setText(String.valueOf(thumbNum));
//                        layoutParams.width = DeviceUtils.dip2px(context,40);
//                    }else{
//                        thumbMsgNumText.setText(R.string.more_than_99);
//                        layoutParams.width = DeviceUtils.dip2px(context,54);
//                    }
//                    thumbMsgNumText.setLayoutParams(layoutParams);
//                }else{
//                    thumbMsgNumText.setVisibility(View.INVISIBLE);
//                }
//                if(commentNum >0){
//                    commentMsgNumText.setVisibility(View.VISIBLE);
//                    ViewGroup.LayoutParams layoutParams = commentMsgNumText.getLayoutParams();
//                    layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//                    if(commentNum<10){
//                        commentMsgNumText.setText(String.valueOf(commentNum));
//                        layoutParams.width =  ViewGroup.LayoutParams.WRAP_CONTENT;
//                    }else if( commentNum< 99){
//                        commentMsgNumText.setText(String.valueOf(commentNum));
//                        layoutParams.width = DeviceUtils.dip2px(context,40);
//                    }else{
//                        commentMsgNumText.setText(R.string.more_than_99);
//                        layoutParams.width = DeviceUtils.dip2px(context,54);
//                    }
//                    commentMsgNumText.setLayoutParams(layoutParams);
//                }else{
//                    commentMsgNumText.setVisibility(View.INVISIBLE);
//                }

            }
        };
        fetchMessageTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.MESSAGE_NUMBER_URL);
    }

}
