package com.yidianling.course;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.ydl.ydlcommon.base.BaseActivity;
import com.yidianling.course.courseNew.home.CourseHomeFragment;
import com.yidianling.course.courseNew.mine.MyCourseActivity;
import com.yidianling.course.router.CourseIn;
import com.yidianling.course.widget.SearchBarView;

/**
 * 课程首页Activity（心理课程）
 */
@Route(path = "/course/home")
public class CourseActivity extends BaseActivity {

    @BindView(R2.id.content)
    FrameLayout content;
    @BindView(R2.id.img_music)
    ImageView imgMusic;
    @BindView(R2.id.iv_back)
    ImageView ivBack;
    @BindView(R2.id.tv_left_menu)
    TextView leftMenu;
    @BindView(R2.id.activity_course_search_bar)
    SearchBarView activityCourseSearchBar;

    private boolean isSplash;//是否从启动页进入
    private final static String TAB = "tab";
    public final static int TAB0 = 0;


    private static final String IS_SPLASH = "isSplash";


    public static void start(Context context, int tab) {
        Intent intent = new Intent(context, CourseActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(TAB, tab);
        intent.putExtra("bundle", bundle);
        context.startActivity(intent);
    }

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


    @Override
    protected void initDataAndEvent() {
        ButterKnife.bind(this);

        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (bundle != null) {
            isSplash = bundle.getBoolean("isSplash", false);
        }

        activityCourseSearchBar.initBarType(SearchBarView.JUMP_TO_SEARCH_COURSE, null);

        init();
    }

    private void init() {
        initTags();

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        imgMusic.setOnClickListener(v -> {
            if (YDLMusicHelper.INSTANCE.isCoursePlaying()) {
                //课程播放页
                YDLMusicHelper.INSTANCE.startCoursePlayActivity(getMContext(), 1, 0, "", false);
            } else {
                imgMusic.setVisibility(View.GONE);
            }

        });
        leftMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CourseIn.INSTANCE.isLogin()) {
                    MyCourseActivity.start(CourseActivity.this);
                } else {
                    startActivity(CourseIn.INSTANCE.loginWayIntent(CourseActivity.this));
                }
            }
        });
    }

    /**
     * 初始化tab
     */
    private void initTags() {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.content);
        if (fragment == null) {
            fragment = new CourseHomeFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.content, fragment)
                    .commit();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (YDLMusicHelper.INSTANCE.isCoursePlaying()) {
            imgMusic.setVisibility(View.VISIBLE);
        } else {
            imgMusic.setVisibility(View.GONE);
        }
    }

    public void disPro() {
        dismissProgressDialog();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (isSplash) {
            startActivity(CourseIn.INSTANCE.mainIntent(this));
            finish();
        }
    }
}
