package com.jld.vod.view.fragment

import android.content.Intent
import android.widget.Button
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.jld.vod.R
import com.jld.vod.base.BaseFragment
import com.jld.vod.model.bean.MovieByMidBean
import com.jld.vod.utils.LogUtils
import com.jld.vod.view.VideoPlayActivity
import com.jld.vod.viewmodel.MovieDetailsViewModel
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.enums.CoreAnim
import com.xuexiang.xui.widget.actionbar.TitleBar
import kotlinx.android.synthetic.main.fragment_movie_details.*
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

@Page(anim = CoreAnim.none)
class MovieDetailsFragment : BaseFragment() {

    private var mBtPlay:Button?= null

    override fun getLayoutId(): Int {
        return R.layout.fragment_movie_details
    }


    override fun initViews() {
        val bundle = arguments
        val movie: MovieByMidBean = bundle!!.getSerializable("movie") as MovieByMidBean
        val bt_movie_introduce_play_notice = findViewById(R.id.bt_movie_introduce_play_notice) as Button
        val rb_movie_introduce_grade = findViewById(R.id.rb_movie_introduce_grade) as RatingBar
        val iv_movie_cover = findViewById(R.id.iv_movie_cover) as ImageView
        val tv_movieDirector = findViewById(R.id.tv_movieDirector) as TextView
        val tv_movieBirthday = findViewById(R.id.tv_movieBirthday) as TextView
        val tv_movie_movieLead = findViewById(R.id.tv_movie_movieLead) as TextView
        val tv_movie_movieInfo = findViewById(R.id.tv_movie_movieInfo) as TextView
        val tv_movie_movieDuration = findViewById(R.id.tv_movie_movieDuration) as TextView
        val tv_movie_type = findViewById(R.id.tv_movie_type) as TextView

        if (movie.movieImgUrl != "") {
            LogUtils.logD(com.jld.vod.config.Config.ApiBaseUrl + "/synchronization/previewImg?imgUrl=" + movie.movieImgUrl)
                Glide.with(this)
                    .load(com.jld.vod.config.Config.ApiBaseUrl + "/synchronization/previewImg?imgUrl=" + movie.movieImgUrl)
                    .into(iv_movie_cover)
        }
        rb_movie_introduce_grade.rating = movie.movieScore.toFloat() / 2
        tv_movieDirector.setText(movie.movieDirector)
        val movieBirthday = movie.movieBirthday
        if (movieBirthday != "") {
            val date1 = Date(movieBirthday.toLong()) //对应的就是时间戳对应的Dat
            var result2 = SimpleDateFormat("yyyy-MM-dd").format(date1)
            tv_movieBirthday.setText(result2)
        }

        tv_movie_movieLead.setText(movie.movieLead)
        tv_movie_movieInfo.setText(movie.movieInfo)
        tv_movie_movieDuration.setText(movie.movieDuration)
        tv_movie_type.setText(movie.movieLanguage)

        mBtPlay = findViewById(R.id.bt_movie_introduce_play)
        mBtPlay!!.setOnClickListener {
            val intent = Intent(activity, VideoPlayActivity::class.java)
            LogUtils.logD("电影播放界面"+movie.movieFileUrl)
            intent.putExtra("movieFileUrl", movie.movieFileUrl);
            intent.putExtra("movieName", movie.movieName);
            intent.putExtra(
                "movieImgUrl",
                com.jld.vod.config.Config.ApiBaseUrl + "/synchronization/previewImg?imgUrl=" + movie!!.movieImgUrl
            );
            startActivity(intent)
        }


        bt_movie_introduce_play_notice.setOnClickListener {
            this.getActivity()!!.onBackPressed()
        }
    }

    override fun initTitle(): TitleBar? {
        return null
    }
}