package com.example.leudaemialikeme.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.leudaemialikeme.Fragment.CommunityFragment;
import com.example.leudaemialikeme.Fragment.IndexFragment;
import com.example.leudaemialikeme.Fragment.MessageFragment;
import com.example.leudaemialikeme.Fragment.MyFragment;
import com.example.leudaemialikeme.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    //装载Fragment的集合
    private List<Fragment> fragmentList;
    //四个tab对应的布局
    private LinearLayout tabHome;
    private LinearLayout tabCommunity;
    private LinearLayout tabMessage;
    private LinearLayout tabMy;
    //四个 Tab 对应的 ImageButton
    private ImageButton imgHome;
    private ImageButton imgCommunity;
    private ImageButton imgMessage;
    private ImageButton imgMy;
    //四个Tab对应的文本
    private TextView textHome;
    private TextView textCommunity;
    private TextView textMessage;
    private TextView textMy;



    //底部导航点击事件监听器
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            //先将四个 ImageButton 置为灰色
            resetImgs();
            //根据点击的 Tab 切换不同的页面及设置对应的 ImageButton 为绿色
            switch (v.getId()) {
                case R.id.tab_home:
                    selectTab(0);
                    break;
                case R.id.tab_community:
                    selectTab(1);
                    break;
                case R.id.tab_message:
                    selectTab(2);
                    break;
                case R.id.tab_my:
                    selectTab(3);
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        initFragments(); //初始化数据
        initViews(); //初始化控件
        initEvents(); //初始化事件
        initFirstRun(0);//第一次运行初始化界面，第一个碎片

        int flag = getIntent().getIntExtra("flag", 0);
        if (flag == 1) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.frag_layout,new IndexFragment())
                    .addToBackStack(null)
                    .commit();
        }

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                QuestionDao userDao=new QuestionDao();
//                try {
//                    userDao.qSelect();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
    }

    private void initFirstRun(int i) {
        resetImgs(); //重置所有 Tab
        selectTab(i); //显示第 i 个碎片
    }

    private void selectTab(int i) {
        //根据点击的 Tab 设置对应的 ImageButton 为绿色
        switch (i) {
            case 0:
                imgHome.setImageResource(R.mipmap.img_home_pressed);
                textHome.setTextColor(getResources().getColor(R.color.hair_grey));
                break;
            case 1:
                imgCommunity.setImageResource(R.mipmap.img_community_pressed);
                textCommunity.setTextColor(getResources().getColor(R.color.hair_grey));
                break;
            case 2:
                imgMessage.setImageResource(R.mipmap.img_message_pressed);
                textMessage.setTextColor(getResources().getColor(R.color.hair_grey));
                break;
            case 3:
                imgMy.setImageResource(R.mipmap.img_my_pressed);
                textMy.setTextColor(getResources().getColor(R.color.hair_grey));
                break;
        }
        //设置当前点击的 Tab 所对应的页面
        setCurrentFragment(i);
    }

    private void setCurrentFragment(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction trans = fm.beginTransaction();
        trans.replace(R.id.frag_layout, fragmentList.get(i));
        trans.commit();
        //Log.e("fragment", "ok");
    }

    private void resetImgs() {
        imgHome.setImageResource(R.mipmap.img_home_normal);
        imgCommunity.setImageResource(R.mipmap.img_community_normal);
        imgMessage.setImageResource(R.mipmap.img_message_normal);
        imgMy.setImageResource(R.mipmap.img_my_normal);
        textHome.setTextColor(getResources().getColor(R.color.light_grey));
        textCommunity.setTextColor(getResources().getColor(R.color.light_grey));
        textMessage.setTextColor(getResources().getColor(R.color.light_grey));
        textMy.setTextColor(getResources().getColor(R.color.light_grey));
    }

    private void initEvents() {
        tabHome.setOnClickListener(onClickListener);
        tabCommunity.setOnClickListener(onClickListener);
        tabMessage.setOnClickListener(onClickListener);
        tabMy.setOnClickListener(onClickListener);

    }

    private void initViews() {
        tabHome = (LinearLayout)findViewById(R.id.tab_home);
        tabCommunity = (LinearLayout)findViewById(R.id.tab_community);
        tabMessage = (LinearLayout)findViewById(R.id.tab_message);
        tabMy = (LinearLayout)findViewById(R.id.tab_my);
        imgHome = (ImageButton)findViewById(R.id.tab_home_img);
        imgCommunity = (ImageButton)findViewById(R.id.tab_community_img);
        imgMessage = (ImageButton)findViewById(R.id.tab_message_img);
        imgMy = (ImageButton)findViewById(R.id.tab_my_img);
        textHome = (TextView)findViewById(R.id.tab_home_text);
        textCommunity = (TextView)findViewById(R.id.tab_community_text);
        textMessage = (TextView)findViewById(R.id.tab_message_text);
        textMy = (TextView)findViewById(R.id.tab_my_text);

    }

    private void initFragments() {
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new IndexFragment());
        fragmentList.add(new CommunityFragment());
        fragmentList.add(new MessageFragment());
        fragmentList.add(new MyFragment());
    }

    public Fragment getFragment(int i){
        return fragmentList.get(i);
    }
}