package com.yutou.jianr_mg.views;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import com.yutou.jianr_mg.Adapters.ViewPagerAdapter;
import com.yutou.jianr_mg.Fragments.GameDataFragments.WarLogFragments.DaytimeFragment;
import com.yutou.jianr_mg.Fragments.GameDataFragments.WarLogFragments.NightFragment;
import com.yutou.jianr_mg.Fragments.GameDataFragments.WarLogFragments.WarLogInterface;
import com.yutou.jianr_mg.R;


import org.json.JSONObject;

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

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class WarLogActivity extends AppCompatActivity {
    JSONObject json;
    private PageNavigationView tab;
    private ViewPager pager;
    private NavigationController navigationController;


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

        try {
            json = new JSONObject(Objects.requireNonNull(getIntent().getStringExtra("data")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (json == null) {
            json = new JSONObject();
        }

        initView();
    }

    private void initView() {
        tab = findViewById(R.id.tab);
        pager = findViewById(R.id.pager);
        List<Fragment> list = new ArrayList<>();
        list.add(DaytimeFragment.getInstance());
        list.add(NightFragment.getInstance());
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), list);
        pager.setAdapter(adapter);
        pager.setCurrentItem(0);
        navigationController = tab.material()
                .addItem(R.drawable.icon_daytime, R.drawable.icon_daytime_select, "战斗过程")
                .addItem(R.drawable.icon_night, R.drawable.icon_night_select, "战斗报告")
                .build();
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                pager.setCurrentItem(index);
                ((WarLogInterface)list.get(index)).setData(json);
            }

            @Override
            public void onRepeat(int index) {

            }
        });
        navigationController.setupWithViewPager(pager);
        navigationController.setSelect(0);
        //message
        ((WarLogInterface)list.get(0)).setData(json);

    }
}
