package com.wasu.cs.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.exoplayer.C;
import com.wasu.cs.model.ChildrenAllLikeData;
import com.wasu.cs.mvp.IView.IChildrenAllLikeView;
import com.wasu.cs.mvp.presenter.ChildrenAllLikePresenter;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.module.log.WLog;

import java.util.List;

import cn.com.wasu.main.IntentConstant;
import cn.com.wasu.main.IntentMap;
import cn.com.wasu.main.R;

/**
 * Created by jeepc on 2016/10/24.
 */

public class ActivityChildrenAllLike extends ActivityBase implements IChildrenAllLikeView, View.OnClickListener {
    private String TAG = "ActivityChildrenAllLike";

    private LinearLayout row1, row2, row3;

    private ImageView search, fav, bg;

    private ChildrenAllLikePresenter presenter;

    private ChildrenAllLikeData mData;


    private String mUrl = "http://120.26.137.228/?s=2002&p=sntEntryAK&k=1&v=3&stype=27&subjectId=68&bodyId=311&rt1=1&rt2=2&rt3=1";

    private void parseIntent() {
        mUrl = getIntent().getStringExtra(IntentConstant.DATAURI.value());
    }

    @Override
    protected void doCreate(Bundle savedInstanceState) {
        WLog.i(TAG, "doCreate()");

        setContentView(R.layout.activity_children_all_like);
        initView();
        parseIntent();
        presenter = new ChildrenAllLikePresenter();
        presenter.attachView(this);
        presenter.loadData(mUrl);
    }


    @Override
    protected void setDefaultBg(Drawable drawable) {

    }

    private void initView() {
        row1 = (LinearLayout) findViewById(R.id.row1);
        row2 = (LinearLayout) findViewById(R.id.row2);
        row3 = (LinearLayout) findViewById(R.id.row3);
        search = (ImageView) findViewById(R.id.search);
        search.setOnClickListener(this);
        fav = (ImageView) findViewById(R.id.fav);
        fav.setOnClickListener(this);
        bg = (ImageView) findViewById(R.id.bg_iv);
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inPreferredConfig = Bitmap.Config.RGB_565;
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.children_all_like_bg,options);
//        bg.setImageBitmap(bitmap);
        FrescoImageFetcherModule.getInstance().attachImage("res://cn.com.wasu.main/" + R.drawable.children_all_like_bg, bg);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
            presenter = null;
        }
    }

    @Override
    public void onDataLoaded(ChildrenAllLikeData childrenData) {
        mData = childrenData;
        setRow1Data(childrenData.getRow1());
        setRow2Data(childrenData.getRow2());
        setRow3Data(childrenData.getRow3());
    }


    private void scaleView(View view, float f) {
        view.setScaleX(f);
        view.setScaleY(f);
    }


    View.OnClickListener cl = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ChildrenAllLikeData.ListBean listBean = (ChildrenAllLikeData.ListBean) v.getTag();
            if (listBean != null) {
                if (listBean.getLayout().equals("Detail_Series") || listBean.getLayout().equals("Detail_Movie")) {
                    Intent intent = new Intent();
                    IntentMap.startIntent(ActivityChildrenAllLike.this, intent, null, listBean.getJsonUrl(), ActivityChildrenDetail.class);
                } else {
                    IntentMap.startIntent(ActivityChildrenAllLike.this, null, listBean.getLayout(), listBean.getJsonUrl(), null);
                }
            }
        }
    };

    private void setRow1Data(List<ChildrenAllLikeData.ListBean> data) {
        for (int i = 0; i < data.size(); i++) {
            final View view = LayoutInflater.from(this).inflate(R.layout.item_children_all_like_row1, null);
            View crow = view.findViewById(R.id.crown);
            TextView name = (TextView) view.findViewById(R.id.name);
            name.setText(data.get(i).getTitle());
            ImageView pic = (ImageView) view.findViewById(R.id.pic);
            if (i == 2) {
                pic.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN) {
                            if (row2.getChildAt(0) != null) {
                                row2.getChildAt(0).requestFocus();
                            }
                            return true;
                        }
                        return false;
                    }
                });
            }
            pic.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        scaleView(view, 1.1f);
                    } else {
                        scaleView(view, 1f);
                    }
                }
            });
            if (i == 0) {
                crow.setBackgroundResource(R.drawable.top1);
                view.requestFocus();
            } else if (i == 1) {
                crow.setBackgroundResource(R.drawable.top2);
            } else if (i == 2) {
                crow.setBackgroundResource(R.drawable.top3);
            }
            FrescoImageFetcherModule.getInstance().attachImage(data.get(i).getPicUrl(),
                    pic, (int) getResources().getDimension(R.dimen.d_7dp));

            pic.setTag(data.get(i));
            pic.setOnClickListener(cl);
            row1.addView(view);
        }
    }

    private void setRow2Data(List<ChildrenAllLikeData.ListBean> data) {
        final int count = data.size();
        for (int i = 0; i < count; i++) {
            final View view = LayoutInflater.from(this).inflate(R.layout.item_children_all_like_row2, null);
            TextView name = (TextView) view.findViewById(R.id.name);
            name.setText(data.get(i).getTitle());
            ImageView pic = (ImageView) view.findViewById(R.id.pic);
            FrescoImageFetcherModule.getInstance().attachImage(data.get(i).getPicUrl(),
                    pic, (int) getResources().getDimension(R.dimen.d_7dp));
            if (i == count - 1) {
                pic.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN) {
                            if (row3.getChildAt(0) != null) {
                                row3.getChildAt(0).requestFocus();
                            }
                            return true;
                        }
                        return false;
                    }
                });
            }
            pic.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        scaleView(view, 1.1f);
                    } else {
                        scaleView(view, 1f);
                    }
                }
            });
            pic.setTag(data.get(i));
            pic.setOnClickListener(cl);
            row2.addView(view);
        }
    }


    private void setRow3Data(List<ChildrenAllLikeData.ListBean> data) {
        final int count = data.size();
        for (int i = 0; i < count; i++) {
            final View view = LayoutInflater.from(this).inflate(R.layout.item_children_all_like_row2, null);
            TextView name = (TextView) view.findViewById(R.id.name);
            name.setText(data.get(i).getTitle());
            ImageView pic = (ImageView) view.findViewById(R.id.pic);
            FrescoImageFetcherModule.getInstance().attachImage(data.get(i).getPicUrl(),
                    pic, (int) getResources().getDimension(R.dimen.d_7dp));
            final int finalI = i;
            pic.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (finalI == count - 1 && keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (row1.getChildAt(0) != null) {
                            row1.getChildAt(0).requestFocus();
                        }
                        return true;
                    }
                    if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) {
                        switch (finalI) {
                            case 0:
                            case 1:
                                if (row1.getChildAt(0) != null) {
                                    row1.getChildAt(0).requestFocus();
                                }
                                break;
                            case 2:
                            case 3:
                                if (row1.getChildAt(1) != null) {
                                    row1.getChildAt(1).requestFocus();
                                }
                                break;
                            case 4:
                                if (row1.getChildAt(2) != null) {
                                    row1.getChildAt(2).requestFocus();
                                }
                                break;
                            default:
                                break;
                        }

                        return true;
                    }
                    return false;
                }
            });
            pic.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        scaleView(view, 1.1f);
                    } else {
                        scaleView(view, 1f);
                    }
                }
            });
            pic.setTag(data.get(i));
            pic.setOnClickListener(cl);
            row3.addView(view);
        }
    }


    @Override
    public void showErrorExitDlg() {

    }

    @Override
    public boolean uiIsFinishing() {
        return isFinishing();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.search) {
            if (mData != null && !TextUtils.isEmpty(mData.getSearchUrl())) {
                IntentMap.startIntent(ActivityChildrenAllLike.this, null, mData.getLayout(), mData.getSearchUrl(), null);
            }
        } else if (id == R.id.fav) {
            Intent intent = new Intent();
            IntentMap.startIntent(ActivityChildrenAllLike.this, intent, null, null, ActivityChildrenFav.class);
        }
    }
}
