package hunnu.sj.raise_money;

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

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

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.fragment.app.ListFragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

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;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    private AppBarConfiguration mAppBarConfiguration;
    private Toolbar toolbar;
    private ActionBarDrawerToggle mToggle;
    private DrawerLayout mDrawerlayout;
    private NavigationView navigationView;
    private
    TextView menu_name;
    TextView menu_role;
    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;
        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.getName());
            menu_role.setText(user.getRole());
        }
        /*Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        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.getName());
            menu_role.setText(user.getRole());
        }*/

        //setContentView(R.layout.content_main);
        //new UploadThread().start();
        lv_news = findViewById(R.id.list);
        lv_news.setAdapter(newsAdapter);
        lv_news.setOnItemClickListener(this);
        News news = new News();
        news.setTitle("火箭发射成功");
        news.setDes("地方上的房贷首付读书首付第三方的手房贷首付第三方的手负担");
        news.setHead_icon(ContextCompat.getDrawable(this, R.drawable.ic_launcher_background));
        list.add(news);
        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 url = "www.baidu.com";
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private class UploadThread extends Thread{
        @Override
        public void run() {
            Message message = mhandler.obtainMessage();
            int isload = NewsUtils.getAllNews(mContext,list);
            message.what = isload;
            mhandler.sendMessage(message);
            //newsAdapter.notifyDataSetChanged();
        }
    }


    /*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,BrowseRecordActivity.class);
        startActivity(intent);
    }


}
