package com.cooleshow.teacher.widgets;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.bean.HomeHotMusicSheetBean;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Author by pq, Date on 2022/8/8.
 */
public class HomeHotMusicSheetItemView extends FrameLayout {
    public static final String CHARGE_TYPE_FREE = "FREE";
    public static final String CHARGE_TYPE_VIP = "VIP";
    public static final String CHARGE_TYPE_CHARGE = "CHARGE";
    private TextView mTvTag;
    private TextView mTvName;
    private TextView mTvAuthor;
    private TextView mTvUploadAuthor;
    private LinearLayout mLlMusicSubject;
    private View mViewLine;
    private HomeHotMusicSheetBean.RowsBean mBean;

    public HomeHotMusicSheetItemView(@NonNull Context context) {
        this(context, null);
    }

    public HomeHotMusicSheetItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public HomeHotMusicSheetItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_hot_music_sheet_child_layout, this);
        mTvTag = findViewById(R.id.tv_tag);
        mTvName = findViewById(R.id.tv_name);
        mTvAuthor = findViewById(R.id.tv_author);
        mTvUploadAuthor = findViewById(R.id.tv_upload_author);
        mLlMusicSubject = findViewById(R.id.ll_music_subject);
        mViewLine = findViewById(R.id.view_line);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBean != null) {
                    if (UiUtils.isFastClick()) {
                        return;
                    }
                    ARouter.getInstance().build(RouterPath.WebCenter.ACTIVITY_ACCOMPANY_HTML)
                            .withString("url", String.format(WebConstants.SCORE_DETAIL_PAGE, mBean.id))
                            .withBoolean("isHideTitle", true)
                            .withBoolean("statusBarTextColor", true)
                            .withBoolean("isOpenLight", true)
                            .withInt("orientation", 0)
                            .navigation();
                }
            }
        });

    }

    public void setData(HomeHotMusicSheetBean.RowsBean data) {
        this.mBean = data;
        //曲谱名称
        if (mTvName != null) {
            mTvName.setText(data.musicSheetName);
        }
        //作者
        if (mTvAuthor != null) {
            mTvAuthor.setText(String.format("-%s", data.composer));
        }
        //上传人
        if (mTvUploadAuthor != null) {
            mTvUploadAuthor.setText(String.format("上传者：%s", data.addName));
        }

        mLlMusicSubject.removeAllViews();
        if (!TextUtils.isEmpty(data.subjectNames)) {
            String[] split = data.subjectNames.split(",");
            if (split != null && split.length > 0) {
                for (int i = 0; i < split.length; i++) {
                    createSubjectView(split[i]);
                }
            }
        }

        if (TextUtils.equals(data.chargeType, CHARGE_TYPE_FREE)) {
            mTvTag.setText("免费");
            mTvTag.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_01b84f));
            mTvTag.setBackgroundResource(R.drawable.shape_01b84f_border);
            return;
        }
        if (TextUtils.equals(data.chargeType, CHARGE_TYPE_VIP)) {
            mTvTag.setText("会员");
            mTvTag.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_cd863e));
            mTvTag.setBackgroundResource(R.drawable.shape_cd863e_border);
            return;
        }
        if (TextUtils.equals(data.chargeType, CHARGE_TYPE_CHARGE)) {
            mTvTag.setText("点播");
            mTvTag.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_3591ce));
            mTvTag.setBackgroundResource(R.drawable.shape_3591ce_border);
        }
    }

    private void createSubjectView(String text) {
        TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(com.cooleshow.base.R.layout.view_subject_item_layout, mLlMusicSubject, false);
        textView.setText(text);
        textView.setTextColor(getContext().getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        textView.setBackgroundResource(com.cooleshow.base.R.drawable.shape_effbf9_9dp);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (mLlMusicSubject.getChildCount() >= 1) {
            params.setMargins(SizeUtils.dp2px(4), 0, 0, 0);
        }
        mLlMusicSubject.addView(textView, params);
    }

    public void setDividingLineIsShow(boolean isShow) {
        if (mViewLine != null) {
            mViewLine.setVisibility(isShow ? View.VISIBLE : View.INVISIBLE);
        }
    }
}
