package com.yidianling.course.courseNew.mine;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ydl.base_module.ui.searchbarview.SearchBarView;
import com.yidianling.course.CourseListFragment;
import com.yidianling.course.R;
import com.yidianling.ydlcommon.base.BaseActivity;
import com.yidianling.ydlcommon.ui.confideredpacket.ConfideRedPacketView;
import com.yidianling.ydlcommon.utils.DisplayUtils;
import com.yidianling.ydlcommon.utils.UMEventUtils;

import java.lang.reflect.Field;

/**
 * Created by Jim on 2018/7/16 0016.
 * 心理课程
 */

public class MyCourseActivity extends BaseActivity {

    public final static String TAB_KEY = "tab_index";
    public final static int TAB_PAIED = 2;
    public final static int TAB_COLLECT = 1;

    private int tabIndex;


    public static void start(Context context) {
        Intent i = new Intent(context, MyCourseActivity.class);
        i.putExtra(TAB_KEY, TAB_PAIED);
        context.startActivity(i);
    }

    public static void start(Context context, int index) {
        Intent i = new Intent(context, MyCourseActivity.class);
        i.putExtra(TAB_KEY, index);
        context.startActivity(i);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_my_course;
    }

    @Override
    protected void initDataAndEvent() {
        tabIndex = getIntent().getIntExtra(TAB_KEY, -1);
        TextView tv = findViewById(R.id.tv_title);
        tv.setText("我的课程");
        ImageView iv = findViewById(R.id.iv_back);
        findViewById(R.id.tv_left_menu).setVisibility(View.GONE);
        iv.setOnClickListener(view -> {
            finish();
        });
        SearchBarView searchBarView = findViewById(R.id.search_bar);
        searchBarView.initBarType(SearchBarView.JUMP_TO_SEARCH_COURSE, null);
        TabLayout tabLayout = findViewById(R.id.my_courses_tabs);
        ViewPager viewPager = findViewById(R.id.my_course_vp);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(new MyCourseFragmentAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(2);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 15, 15);
            }
        });

        //初始化倾诉红包
        initConfideRedPacketView();
    }

    private void initConfideRedPacketView() {
        ConfideRedPacketView confideRedPacketView = findViewById(R.id.confideRedPacketView);
        confideRedPacketView.initData(View.VISIBLE);
        confideRedPacketView.setConfide();
    }


    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = DisplayUtils.dip2px(this, left);
            params.rightMargin = DisplayUtils.dip2px(this, right);
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    static class MyCourseFragmentAdapter extends FragmentPagerAdapter {

        private Fragment[] mFragments;
        private String[] titles = new String[]{"收藏", "已购"};

        public MyCourseFragmentAdapter(FragmentManager fm) {
            super(fm);
            mFragments = new Fragment[]{CourseListFragment.newInstance(2), CourseListFragment.newInstance(1)};
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                UMEventUtils.umEvent("我的课程", "收藏", "收藏");
            } else {
                UMEventUtils.umEvent("我的课程", "已购", "已购");
            }

            return mFragments[position];
        }

        @Override
        public int getCount() {
            return mFragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
