package com.mjsheng.myappstore.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.adapter.TopRecyclerViewAdapter;
import com.mjsheng.myappstore.bean.AppContentInfo;
import com.mjsheng.myappstore.dialog.AppImageDialog;
import com.mjsheng.myappstore.utils.Utils;
import com.mjsheng.myappstore.view.CollapsibleTextView;
import com.mjsheng.myappstore.view.RecycleLinearLayoutManager;
import com.mjsheng.myappstore.widget.Sound;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class IntroductionPageFragment extends Fragment {

    private AppContentInfo mIntroductionInfo = null;
    private TextView mVersionTV, mLatestTV;
    private RecyclerView mTopRlView;
    private TopRecyclerViewAdapter mAdapter;
    private CollapsibleTextView mDescriptionTV;
    private Sound sound;
    private AppImageDialog dialog = null;


    public static String getFormatedDateTime(long dateTime) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return sDateFormat.format(new Date(dateTime * 1000));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.introduction_page_layout, container,
                false);
        initView(view);
        sound = new Sound(getContext());
        return view;
    }

    private void initView(View view) {
        mTopRlView = (RecyclerView) view.findViewById(R.id.id_RecyclerView);

        RecycleLinearLayoutManager manager = new RecycleLinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        manager.setSmoothScrollbarEnabled(true);
        mTopRlView.setLayoutManager(manager);
        mTopRlView.setNestedScrollingEnabled(false);
        mDescriptionTV = (CollapsibleTextView) view.findViewById(R.id.detail_content);
        mVersionTV = (TextView) view.findViewById(R.id.detail_version);
        mLatestTV = (TextView) view.findViewById(R.id.detail_latesttime);
        mIntroductionInfo = (AppContentInfo) getArguments().get("info");
        notifyDataSetChanged();
    }

    public void setIntroductionInfo(AppContentInfo introductionInfo) {
        this.mIntroductionInfo = introductionInfo;
    }

    public void notifyDataSetChanged() {
        updateView();
    }

    public void updateView() {
        if (mIntroductionInfo != null) {
            if (mTopRlView != null && mIntroductionInfo.getApp_bigimg1() != null) {
                final List<String> vpList = new ArrayList<>();
                if (!mIntroductionInfo.getApp_bigimg1().equals("")) {
                    vpList.add(mIntroductionInfo.getApp_bigimg1());
                }
                if (!mIntroductionInfo.getApp_bigimg2().equals("")) {
                    vpList.add(mIntroductionInfo.getApp_bigimg2());
                }
                if (!mIntroductionInfo.getApp_bigimg3().equals("")) {
                    vpList.add(mIntroductionInfo.getApp_bigimg3());
                }
                if (!mIntroductionInfo.getApp_bigimg4().equals("")) {
                    vpList.add(mIntroductionInfo.getApp_bigimg4());
                }
                if (!mIntroductionInfo.getApp_bigimg5().equals("")) {
                    vpList.add(mIntroductionInfo.getApp_bigimg5());
                }
                if (vpList != null && vpList.size() > 0) {
                    mAdapter = new TopRecyclerViewAdapter(getActivity(), "appdetail", vpList, new TopRecyclerViewAdapter.HorizontalScrollViewListener() {
                        @Override
                        public void onClick(String iconUrl, int position) {
                            sound.playSoundEffect();
                            for (int i = 0; i < vpList.size(); i++) {
                                String imageUrl = vpList.get(i);
                                if (imageUrl.equals(iconUrl)) {
                                    dialog = new AppImageDialog(getActivity(), vpList, i);
                                    dialog.show();
                                    return;
                                }
                            }
                        }
                    });
                    mTopRlView.setAdapter(mAdapter);
                }
            }
            if (mDescriptionTV != null && mIntroductionInfo !=null && mIntroductionInfo.getApp_desc() != null) {
                mDescriptionTV.setContent(Utils.ToDBC(mIntroductionInfo.getApp_desc()));
            }
            if (mVersionTV != null) {
                String version = mIntroductionInfo.getApp_banben().replaceAll("[a-zA-Z]", "").trim();
                if (!TextUtils.isEmpty(version)) {
                    if (!version.contains("v") || !version.contains("V")) {
                        mVersionTV.setText("v" + version);
                    } else {
                        mVersionTV.setText(version);
                    }
                } else {
                    mVersionTV.setText("");
                }
            }
            if (mLatestTV != null) {
                mLatestTV.setText(getFormatedDateTime(Long.parseLong(mIntroductionInfo.getApp_time())));
            }

        }
    }
}
