package com.ewei.demo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.ewei.demo.article.ArticleTopicListActivity;
import com.ewei.demo.article.QuestionTopicListActivity;
import com.ewei.demo.article.RecommendListActivity;
import com.ewei.demo.chat.ChatActivity;
import com.ewei.sdk.base.EweiWebSocket;
import com.ewei.sdk.chat.ChatUtils;
import com.ewei.sdk.receiver.EweiReceiver;
import com.ewei.sdk.ticket.SubmitTicketActivity;
import com.ewei.demo.ticket.TicketListActivity;
import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.base.EweiConfig;

public class MainActivity extends BaseActivity {

    private MainBroadcasts myBroadcasts;

    private String accountInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        accountInfo = getIntent().getStringExtra("accountInfo");

        //初始化用户
//        EweiConfig.initUserAccount("123321",null);

        if (myBroadcasts == null) {
            myBroadcasts = new MainBroadcasts();
        }
        this.registerReceiver(myBroadcasts, MainBroadcasts.getIntentFilter());
        initView();
    }

    private void initView() {
        LinearLayout chatInfo = (LinearLayout) findViewById(R.id.ewei_sdk_main_ui_chat);
        LinearLayout recommendInfo = (LinearLayout) findViewById(R.id.ewei_sdk_main_recommend_article);
        LinearLayout ticketInfo = (LinearLayout) findViewById(R.id.ewei_sdk_main_ui_ticket);
        LinearLayout viewInfo = (LinearLayout) findViewById(R.id.ewei_sdk_main_ui_view);
        LinearLayout articleTopicInfo = (LinearLayout) findViewById(R.id.ewei_sdk_article_topic_view);
        LinearLayout questionTopicInfo = (LinearLayout) findViewById(R.id.ewei_sdk_question_topic_view);

        recommendInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RecommendListActivity.class);
                startActivity(intent);
            }
        });

        chatInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatUtils.starChatActivity(MainActivity.this, ChatActivity.class);
            }
        });

        ticketInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SubmitTicketActivity.startActivity(MainActivity.this);
            }
        });

        viewInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TicketListActivity.class);
                startActivity(intent);
            }
        });

        articleTopicInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ArticleTopicListActivity.class));
            }
        });

        questionTopicInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, QuestionTopicListActivity.class));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EweiWebSocket.getInstance().stopWebSocket();
        if (myBroadcasts != null) {
            this.unregisterReceiver(myBroadcasts);
        }
    }

    private class MainBroadcasts extends EweiReceiver {

        @Override
        public void onTokenMessage(String messageInfo) {
            //重新初始化用户
            EweiConfig.initUserAccount(accountInfo, null);
        }
    }
}
