package hunnu.sj.raise_money;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.content.ContextCompat;
import androidx.navigation.ui.AppBarConfiguration;

import com.google.android.material.navigation.NavigationView;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import hunnu.sj.raise_money.news.News;
import hunnu.sj.raise_money.news.NewsAdapter;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    private AppBarConfiguration mAppBarConfiguration;
    private Toolbar toolbar;
    private ActionBarDrawerToggle mToggle;
    private DrawerLayout mDrawerlayout;
    private NavigationView navigationView;
    private User user;
    ArrayList<News> list = new ArrayList<>();
    Context mContext;
    private NewsAdapter newsAdapter;
    private ListView lv_news;
    private Handler mhandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                newsAdapter.notifyDataSetChanged();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        user = (User) intent.getSerializableExtra("user");
        setContentView(R.layout.activity_main);
        mContext = this;
        newsAdapter = new NewsAdapter(this,list);
        init();
        if(navigationView.getHeaderCount() > 0) {//设置侧滑栏的显示的值
            View header = navigationView.getHeaderView(0);
            TextView menu_name = header.findViewById(R.id.user_name);
            TextView menu_role = header.findViewById(R.id.user_role);
            menu_name.setText(user.getUsername());
            menu_role.setText(user.getRole());
        }
        lv_news = findViewById(R.id.list);
        lv_news.setAdapter(newsAdapter);
        lv_news.setOnItemClickListener(this);
        new UploadThread().start();
    }


    private void init(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mDrawerlayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        toolbar.setTitle("");
        mToggle = new ActionBarDrawerToggle(this, mDrawerlayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawerlayout.addDrawerListener(mToggle);
        mToggle.syncState();

        mDrawerlayout.setDrawerListener(mToggle);
        navigationView = findViewById(R.id.nav_view);
    }
    @Override//点击条目时调用该方法，parent指listview，position指条目位置
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        News news = (News)parent.getItemAtPosition(position);
        String title=news.getName();
        String des=news.getDescri();
        long stu_id = news.getStu_id();
        Intent intent = new Intent(this,NewsContentActivity.class);
        intent.putExtra("news_title",title);
        intent.putExtra("news_des",des);
        intent.putExtra("stu_id",stu_id);
        intent.putExtra("user_id",user.getUser_id());
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private class UploadThread extends Thread{
        @Override
        public void run() {
            Message message = mhandler.obtainMessage();
            int isload = InfoUtils.getAllNews(mContext,list);
            message.what = isload;
            mhandler.sendMessage(message);//加载数据库成功返回1，并且给handler发送信息，刷新list的变量
        }
    }


    /*public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }*/

    public void onNeedHelp(MenuItem item){//侧滑栏的item，用于提交信息
        Intent intent = new Intent(this, UploadActivity.class);
        startActivity(intent);
    }

    public void onBrowseDonateRecord(MenuItem item){
        Intent intent = new Intent(this, BrowseDonateRecordActivity.class);
        long user_id = user.getUser_id();
        intent.putExtra("user_id",user_id);
        startActivity(intent);
    }


}
